[Solved] How to Access Child Item In JSON Array


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

7

solved How to Access Child Item In JSON Array