1

我有来自 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": [ ]如果响应不是包装在- 容器中,这显然会起作用。但是我如何告诉 gsondata在 JSON 响应中查看呢?

4

1 回答 1

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;
    }
}
于 2012-11-25T17:41:23.747 回答