简体   繁体   中英

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

在此处输入图像描述

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在此处输入图像描述

在此处输入图像描述

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM