如何在JSON数组中访问子项 [英] How to Access Child Item In JSON Array
问题描述
我正在基于某些搜索词查询FlickR,响应为JSON数组.这是根级别以及前两个结果:
I am querying FlickR based on certain search terms, and the response is a JSON Array. Here is the root level along with the first two results:
{
photos: {
page: 1,
pages: 4222,
perpage: 100,
total: "422175",
photo: [
{
id: "28571356563",
owner: "8372889@N03",secret: "c4ca6c4364",
server: "8050",
farm: 9,
title: "95040021.jpg",
ispublic: 1,
isfriend: 0,
isfamily: 0,
url_m: "https://farm9.staticflickr.com/8050/28571356563_c4ca6c4364.jpg",
height_m: "332",
width_m: "500"
},
{
id: "28571342883",
owner: "96125450@N00",
secret: "db35a59412",
server: "8307",
farm: 9,
title: "Red #Sunset #Silhouette #Trees #Photography",
ispublic: 1,
isfriend: 0,
isfamily: 0,
url_m: "https://farm9.staticflickr.com/8307/28571342883_db35a59412.jpg",
height_m: "500",
width_m: "424"
},
当我加载结果时,我将遍历所有项目(总数"图)并加载到RecyclerView中.
When I load the results, I am going to iterate through all items (the "total" figure) and load into a RecyclerView.
最终,我想遍历照片",然后获取每张照片的"url_m".这是我当前通过Retrofit对FlickR API的调用:
Ultimately, I want to iterate through the "photos" and then get the "url_m" for each photo. Here is my current call to the FlickR API through Retrofit:
Call<List<Photo>> call = apiInterface.getImages(mQuery);
call.enqueue(new Callback<List<Photo>>() {
@Override
public void onResponse(Call<List<Photo>> call, Response<List<Photo>> response) {
}
@Override
public void onFailure(Call<List<Photo>> call, Throwable t) {
}
});
}
});
我将如何遍历所有照片并获取每张照片的URL?我为每个模型都设置了模型类,这些模型类完全映射到FlickR API JSON对象:
How would I iterate through all photos and get the URL for each photo? I have my model classes set up for each that map exactly to the FlickR API JSON objects:
推荐答案
我认为您在代码中实现了错误的Retrofit回调.如我所见,您首先收到一个名为photos
的JSONObject,其中包含一个JSONArray photo
,因此您的代码应如下所示:
I think you're implementing wrong Retrofit callback in your code. As I can see you're receiving first a JSONObject called photos
that contains a JSONArray photo
, so your code should look like this
Call<PhotoResult> call = apiInterface.getImages(query);
call.enqueue(new Callback<PhotoResult>() {...}
如您所见,回调对象是PhotoResult
,它是json响应的根级别,您应该在其中检索List<Photo>
集合.
As you can see, the callback object is PhotoResult
that is the root level of your json response, and inside you should retrieve the List<Photo>
collection.
要生成您的POJO,您可以使用此网站
http://www.jsonschema2pojo.org/
To generate your POJOs you can use this website
http://www.jsonschema2pojo.org/
您的POJO应该是这样的
Your POJOs should be like this
public class PhotoResult {
@SerializedName("photos")
@Expose
public Photos photos;
}
public class Photos {
@SerializedName("page")
@Expose
public Integer page;
@SerializedName("pages")
@Expose
public Integer pages;
@SerializedName("perpage")
@Expose
public Integer perpage;
@SerializedName("total")
@Expose
public String total;
@SerializedName("photo")
@Expose
public List<Photo> photo = new ArrayList<Photo>();
}
public class Photo {
...
}
这篇关于如何在JSON数组中访问子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!