Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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

publicclassPhotoResult {
    @SerializedName("photos")
    @Expose
    public Photos photos;
}

publicclassPhotos {
    @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>();
}

publicclassPhoto {
    ...
} 

Post a Comment for "How To Access Child Item In Json Array"