我是新來GSON解析,做了一些例子,但是這一次我的JSON是非常複雜的,它看起來像這樣解析嵌套的Json與GSON
{
"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幫助
你可以發佈你的json嗎? – Rohit5k2
這可能是因爲你沒有初始化你的**清單 **?你能給你的代碼嗎? –
Nemolovich
你是指getter和setter? – Alex