Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
181 views
in Technique[技术] by (71.8m points)

java - Parsing nested Json Objects with Gson

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...