-1

我有以下从 URL 读取的示例响应,需要阅读“对象”解析它们并转换为列表

来自服务器的示例响应:

{
    "meta": {
        "limit": 20,
        "next": null,
        "offset": 0,
        "previous": null,
        "total_count": 2
    },
    "objects": [
        {
            "body": "Body of my review",
            "created_by": {
                "first_name": "Josh",
                "id": 2,
                "last_name": "Bothun",
                "username": "josh.bothun.1"
            },
            "id": 1,
            "location": 1,
            "rating": 2,
            "title": "Title of my review"
        },
        {
            "body": "This place is fantastic!",
            "created_by": {
                "first_name": "Josh",
                "id": 1,
                "last_name": "Bothun",
                "username": "josh.bothun.1"
            },
            "id": 2,
            "location": 1,
            "rating": 5,
            "title": "Another review"
        }
    ]
}

我有以下代码,但无法检索对象列表

{           
    Gson GsonObject = new Gson();

String Test = "{\"meta\": {\"limit\": 20,\"next\": null,\"offset\": 0,\"previous\": null,\"total_count\": 2},\"objects\": [ { \"body\": \"Body of my review\",\"created_by\": { \"first_name\": \"Josh\", \"id\": 2, \"last_name\": \"Bothun\",\"username\": \"josh.bothun.1\"}, \"id\": 1,\"location\": 1, \"rating\": 2, \"title\": \"Title of my review\" }, {  \"body\": \"This place is fantastic!\", \"created_by\": { \"first_name\": \"Josh\", \"id\": 1,  \"last_name\": \"Bothun\", \"username\": \"josh.bothun.1\" }, \"id\": 2,\"location\": 1, \"rating\": 5, \"title\": \"Another review\" } ]}";

Type collectionType = new TypeToken<Collection<TabClass>>(){}.getType();
List<TabClass> enums = GsonObject.fromJson(Test, collectionType);

System.out.println(GsonObject.toJson(enums));            

}

class TabClass
{
  private List meta;
  private List objects;
}
4

1 回答 1

1

使用jsonschema2pojo工具为您的 json 架构提供正确的模型

于 2013-07-26T17:26:15.790 回答