-1

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

9
  • Can you post your json? Commented Jan 6, 2015 at 19:32
  • That maybe because you didn't initialize your List<RItems>? Can you give your code? Commented Jan 6, 2015 at 19:33
  • You mean getters and setters? Commented Jan 6, 2015 at 19:34
  • Your Storages should have a Map<String, RItems>. RItems should have an RItem inside it I guess. Commented Jan 6, 2015 at 19:36
  • What do you mean by map ,a map may help in skiping the 39 number also ? Commented Jan 6, 2015 at 19:39

1 Answer 1

0

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.

Sign up to request clarification or add additional context in comments.

3 Comments

i don't have acces to the Json output ,unforunatly
Perhaps you can consider using org.json libraries instead of GSON.
i was using them earlier , but it take a lot of time to parse,so i want to go to Gson , as i read , it's a lot faster

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.