我有来自Facebook的JSON回复,其结构如下:
{
"data": [
{
"id": "105458566194298_411506355589516",
"message": "...",
"type": "status",
"created_time": "2012-11-25T17:26:41+0000",
"updated_time": "2012-11-25T17:26:41+0000",
"comments": {
"count": 0
},
{
"id": "105458566194298_411506355589516",
"message": "...",
"type": "status",
"created_time": "2012-11-25T17:26:41+0000",
"updated_time": "2012-11-25T17:26:41+0000",
"comments": {
"count": 0
}
]
}
我正在尝试使用Gson库对其进行反序列化:
public class FacebookPost {
public String Message;
public String Created_time;
}
Gson gson = new GsonBuilder().create();
Type listType = new TypeToken<ArrayList<FacebookPost>>() {}.getType();
ArrayList<FacebookPost> userList = gson.fromJson(responseJsonString, listType);
如果响应不是"data": [ ]
- 容器中的包装器,那么这显然会起作用。但是,如何告诉gson在JSON响应中查看data
内部?
答案 0 :(得分:2)
你应该做那样的事情:
InputStream source = retrieveStream(url);
Gson gson = new Gson();
Reader reader = new InputStreamReader(source);
APIResponse response = gson.fromJson(reader, APIResponse.class);
使用:
Public class APIResponse{
ArrayList <FacebookPost> data;
public class FacebookPost {
public String message;
public String created_time;
}
}