Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
913 views
in Technique[技术] by (71.8m points)

java - How to Access Child Item In JSON Array

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:

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...