Java 使用Gson解析带有元和对象的JSON响应
我有以下从URL读取的示例响应,需要读取“对象”解析它们并转换为列表 来自服务器的响应示例:Java 使用Gson解析带有元和对象的JSON响应,java,json,Java,Json,我有以下从URL读取的示例响应,需要读取“对象”解析它们并转换为列表 来自服务器的响应示例: { "meta": { "limit": 20, "next": null, "offset": 0, "previous": null, "total_count": 2 }, "objects": [ { "body": "Body of my review
{
"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;
}
{
Gson GsonObject=new Gson();
目前,偏移量为:0,以前的一个;零零零零零,偏移量为:0,以前的一个;零零,目前的一个;零零,总计数为:2},目前的一个,”总体,”,”,”金属,”,”,”,”,”金属,”,”,”元,”元,”元”的测试:“,{“身体”:“这个地方太棒了!”,“创造者”:“{”名字::“乔希”,“身份证”:1,“姓氏”:“波顿”,“用户名”:“乔希.波顿.1\”,“身份证”:2,,“地点”:1,,“评级”:5,,“头衔”:另一篇评论”;
Type collectionType=new-TypeToken(){}.getType();
List enums=GsonObject.fromJson(Test,collectionType);
System.out.println(GsonObject.toJson(enums));
}
类选项卡类
{
私有列表元;
私有列表对象;
}
使用工具为json模式建立正确的模型您的json不是TabClass
对象的集合。