2015-01-06 106 views
-1
對象

我是新來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幫助

+0

你可以發佈你的json嗎? – Rohit5k2

+0

這可能是因爲你沒有初始化你的**清單 **?你能給你的代碼嗎? – Nemolovich

+0

你是指getter和setter? – Alex

回答

0

這是因爲你有39項:

data 
    '-storage 
     '-39 
      '-items [item...] 

如果您改變 「39」 被 「的changeit」,例如:

... 
"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; 
    ... 

(這是一個例子)

你不能使用數字作爲類名,所以我不知道如何映射一個瞬態元素。

+0

我沒有訪問Json的輸出,很不爽 – Alex

+0

也許你可以考慮使用org.json庫而不是GSON。 – dispake

+0

我早些時候使用過它們,但是它需要花費很多時間來解析,所以我想要去Gson,因爲我讀了它,速度要快很多 – Alex