How to Access Child Item In JSON Array

2019-09-28 06:34发布

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"
            },

When I load the results, I am going to iterate through all items (the "total" figure) and load into a RecyclerView.

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

                }
            });

        }
    });

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:

1条回答
家丑人穷心不美
2楼-- · 2019-09-28 06:36

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>() {...}

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.

To generate your POJOs you can use this website http://www.jsonschema2pojo.org/

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 {
    ...
} 
查看更多
登录 后发表回答