Using Retrofit to access JSON arrays

K20GH picture K20GH · Feb 16, 2014 · Viewed 39.2k times · Source

I thought I understood how to do this, but obviously not. I have my API from Flickr, which begins like so:

jsonFlickrApi({
   "photos":{
      "page":1,
      "pages":10,
      "perpage":100,
      "total":1000,
      "photo":[
         {
            "id":"12567883725",
            "owner":"74574845@N05",
            "secret":"a7431762dd",
            "server":"7458",
            "farm":8,
            "title":"",
            "ispublic":1,
            "isfriend":0,
            "isfamily":0,
            "url_l":"http:\/\/farm8.staticflickr.com\/7458\/12567883725_a7431762dd_b.jpg",
            "height_l":"683",
            "width_l":"1024"
         }

Now the information I need to get is from within the photo array, so what I have been trying to do is:

interface ArtService {

    @GET("/services/rest/?method=flickr.photos.getRecent&extras=url_l&owner_name&format=json")
    PhotosResponse getPhotos();

    public class PhotosResponse {
        Photos photos;
    }

    public class Photos {
        List<Arraz> photo;
    }

    public class Arraz {
        int id;
        String title;
        String owner;
        String url_l;
    }
}

Very clear that I seem to be missing the point, however I am unsure of how to get the information..

Answer

LaSombra picture LaSombra · Apr 22, 2014

I would suggest using http://www.jsonschema2pojo.org. You can paste your JSON and it will generate the POJOs for you.

That should do the trick.