1

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.

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

1 Answer 1

2

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 :)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for answering my question. I've solved the problem of displaying the ingredient data by creating a new String and implementing response.body().getResults().getIngredient().toString() to the string that was created earlier. Now how to display it in the form of a list? Please see my question, I've edited it
You can try String.join("\n",response.body().getResults().getIngredient()); Not 100% sure about this but it should be the easiest way. You can also create some ListView or RecyclerView and display it as normal list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.