解析与GSON嵌套的JSON对象 [英] Parsing nested Json Objects with Gson
本文介绍了解析与GSON嵌套的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的GSON解析,做了一些例子,但是这一次我的JSON是非常复杂的,它看起来像这样
I am new to Gson parsing ,did some examples , but this time my json is much complex it looks like this
{
"message": "Ok",
"code": 200,
"data": {
"storage": {
"39": {
"weight": 22000,
"verificationPackageRequested": null,
"id": 39,
"countryCode": "US",
"photosPackageRequested": null,
"created": "2014-12-30 11:27:57",
"ItemPrice": 224.99,
"ItemTitle": "Apple iPad Mini MF432LL/A (16GB, Wi-Fi, Space Gray )",
"PhotoThumbnail": "/upload/storage-products/b8c2839010a8c5aae21df3b9e57125d0_photoThumbnail.jpg"
"items": [
{
"weight": 22000,
"verificationPackageRequested": null,
"id": 39,
"countryCode": "US",
"photosPackageRequested": null,
"created": "2014-12-30 11:27:57",
"ItemPrice": 224.99,
"ItemTitle": "Apple iPad Mini MF432LL/A (16GB, Wi-Fi, Space Gray )",
"PhotoThumbnail": "/upload/storage-products/b8c2839010a8c5aae21df3b9e57125d0_photoThumbnail.jpg"
}
],
"cellStatus": null,
"orderStorageIsMfPackage": 0,
"storageImage": "/upload/storage/storage_6_thumbnail.jpg"
}
}
},
"error": false
}
我想这样
static class Page{
String message;
int code;
Data data;
//getters+setters
}
static class Data{
HashMap<Values,String> storage;
}
static class Values{
String PhotoThumbnail;
String ItemTitle;
String showPrice;
String weight;
String symbol;
String created;
String id;
String photosPackageRequested;
String verificationPackageRequested;
String countryCode;
//getters+setters
@Override
public String toString(){
return PhotoThumbnail+ " - "+ItemTitle+" - "+showPrice+" - "+weight+" - "+symbol+" - "+created+" - "+id+" - "+photosPackageRequested+" - "+verificationPackageRequested+" -"+countryCode;
}
}
}
和传递这样的结果
Gson gson = new GsonBuilder().create();
Page wp=gson.fromJson(json,Page.class) ;
但是,所有的我不能得到它的工作,它显示存储为空,我不明白为什么,plsease帮助
But all the i can't get it to work , it show storage as null , i can't understand why , plsease help
推荐答案
这是因为你有39项:
data
'-storage
'-39
'-items [item...]
如果您通过更改的changeit中的39为例:
If you change the "39" by "changeIT" for example:
...
"data": {
"storage": {
"changeIT": {
"orderStorageIsAlone": 1,
...
您可以使用模型:
static class Wrapper {
Data data;
}
static class Data {
Storages storage;
}
static class Storages {
ChangeIT changeIT;
}
static class ChangeIT {
List<RItems> items;
public ChangeIT() {
items = new ArrayList<>();
}
}
static class RItems {
String PhotoThumbnail;
String ItemTitle;
String showPrice;
String weight;
String symbol;
String created;
String id;
String photosPackageRequested;
String verificationPackageRequested;
String countryCode;
...
(这是一个例子)
(This is an example)
您不能使用数字作为类名,所以我不知道如何映射一个短暂的元素。
You can not use numbers as Class name, so I don't know how to map a transient element.
这篇关于解析与GSON嵌套的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文