Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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
}

i tried this way

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;
    }
}

}

And pass results like this

Gson gson = new GsonBuilder().create();
Page wp=gson.fromJson(json,Page.class) ;

But all the i can't get it to work , it show storage as null , i can't understand why , plsease help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
173 views
Welcome To Ask or Share your Answers For Others

1 Answer

It is because you have 39 item:

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

If you change the "39" by "changeIT" for example:

...
"data": {
    "storage": {
        "changeIT": {
            "orderStorageIsAlone": 1,
...

You can use model:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...