[英]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.如果 json 数据如下所示,如何在 json object 中显示数组数据。 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这是我想要获取数据的完整 API 地址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.这是我的 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这是android studio显示的错误
Thanks, I hope someone gives an example project that displays array data in a json object like my problem above.谢谢,我希望有人给出一个示例项目,在 json object 中显示数组数据,就像我上面的问题一样。
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:)我希望这会有所帮助:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.