我收到来自Flickr API的JSON响应是这样的:
{
"photos": {
"page": 1,
"pages": 1,
"perpage": 100,
"total": 2,
"photo": [
{
"id": "14774811932",
"owner": "[email protected]",
"secret": "cbb99f0039",
"server": "3853",
"farm": 4,
"title": "Weltschmerz",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0,
"date_faved": "1432471819"
},
{
"id": "13274800365",
"owner": "[email protected]",
"secret": "b652859e6a",
"server": "3688",
"farm": 4,
"title": "cactus desert",
"ispublic": 1,
"isfriend": 0,
"isfamily": 0,
"date_faved": "1432466782"
}
]
},
"stat": "ok"
}
,我需要得到一个ArrayList两个Photo
对象。
我的代码是:
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());
目前我加载从资产的文件。当这个工作我就改成了Flickr API
照片类:
public class Photos {
Integer page;
Integer pages;
Integer perpage;
Integer total;
ArrayList<Photo> photos;
String stat;
//getters and setters here
我的理解是,我必须先提取photos
项目,然后从项目中提取阵列photo
所以我可以得到我需要的ArrayList。
你的照片类是什么样的? –
@BidhanA刚刚用代码更新了答案。 – vicolored