Reputation: 835
I'm getting a Json response from Flickr api which is like :
{
"photos": {
"page": 1,
"pages": 1,
"perpage": 100,
"total": 2,
"photo": [
{
"id": "14774811932",
"owner": "32738276@N08",
"secret": "cbb99f0039",
"server": "3853",
"farm": 4,
"title": "Weltschmerz",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0,
"date_faved": "1432471819"
},
{
"id": "13274800365",
"owner": "114920113@N07",
"secret": "b652859e6a",
"server": "3688",
"farm": 4,
"title": "cactus desert",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0,
"date_faved": "1432466782"
}
]
},
"stat": "ok"
}
and I need to get the two Photo
objects in an ArrayList.
My code is:
Helper helper = new Helper(this);
String jsonString = helper.loadJSONFromAsset();
Gson gson = new Gson();
Photos photos = gson.fromJson(jsonString,
new TypeToken<Photos>() { }.getType());
// ArrayList<Photo> photos = gson.fromJson(jsonString,
// new TypeToken<ArrayList<Photo>>(){}.getType());
Currently I load the file from assets. When this works I'll change it to the flickr api
Photo class :
public class Photos {
Integer page;
Integer pages;
Integer perpage;
Integer total;
ArrayList<Photo> photos;
String stat;
//getters and setters here
My understanding is that I have first to extract the photos
item and then from that item to extract the Array photo
so I can get the ArrayList I need.
Upvotes: 0
Views: 3176
Reputation: 6024
I think you had to create the Gson TypeToken like this:
public class Photos {
Integer page;
Integer pages;
Integer perpage;
Integer total;
ArrayList<Photo> photo;
String stat;
//setters and getters
}
In this way you could replicate the Flickr JSON and Gson will map the Json correctly with:
Photos photos = gson.fromJson(jsonString,
new TypeToken<Photos>(){}.getType());
then you can get your photos ArrayList with:
photos.getPhoto();
Upvotes: 1