Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to parse array inside json object in android java?

how to display array data in json object if the json data is like below. I want to display array data of Ingredient and step. This is the full API Address that I want to fetch the data https://masak-apa-tomorisakura.vercel.app/api/recipe/resep-nasi-bakar-ayam

enter image description here

I’ve tried several ways but I can’t find how to implement it properly.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

this is my json model.

@SerializedName("ingredient")
@Expose
Ingredient ingredient = null;


public Ingredient getIngredient() {
    return ingredient;
}

public void setIngredient(Ingredient ingredient) {
    this.ingredient = ingredient;
}

This is the code I use to display the data

    public void LoadData() {
    Call<ResultsResponse> call = Config.getInstance().getApi().results(kunci);
    call.enqueue(new Callback<ResultsResponse>() {
        @Override
        public void onResponse(Call<ResultsResponse> call, Response<ResultsResponse> response) {
            shimmerFrameLayout.startShimmer();
            detailJudul.setText(response.body().getResults().getTitle());
            detailWaktu.setText(response.body().getResults().getTimes());
            detailKesulitan.setText(response.body().getResults().getDificulty());
            detailPorsi.setText(response.body().getResults().getServings());
            detailDeskripsi.setText(response.body().getResults().getIngredient());
            detailAuthor.setText(response.body().getResults().getAuthor().getUser());
            Glide.with(DetailResepActivity.this)
                    .load(response.body().getResults().getThumb())
                    .apply(new RequestOptions().override(400, 400))
                    .into(detailGambar);

            //Intent Baca Resep di browser
            llVisitWeb.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String url = ("https://www.masakapahariini.com/resep/" + kunci);
                    Intent i = new Intent(Intent.ACTION_VIEW);
                    i.setData(Uri.parse(url));
                    startActivity(i);
                }
            });
            shimmerFrameLayout.setVisibility(View.GONE);
        }

        @Override
        public void onFailure(Call<ResultsResponse> call, Throwable t) {
            Log.d("Hasil", t.getMessage());
        }

    });

    //Intent kembali ke MainActivity
    RelativeLayout ivBack=findViewById(R.id.back);
    ivBack.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(v.getContext(), MainActivity.class);
            startActivity(i);

        }
    });
}

This is the error that android studio shows
enter image description here

enter image description here

Thanks, I hope someone gives an example project that displays array data in a json object like my problem above.

UPDATE

Thanks Gralls, I can display the array data by creating a new string and implementing

String ingredient= response.body().getResults().getIngredient().toString();
detailDeskripsi.setText(ingredient);

it into the previously created string. Then how to make the data ingredient can be in the form of a list?
enter image description here

>Solution :

I don’t have an example project but ingredients is array of strings. So instead of having

@SerializedName("ingredient")
@Expose
Ingredient ingredient = null;

you should parse array into some sort of the list

@SerializedName("ingredient")
List<String> ingredient = null;

I hope that this will help 🙂

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading