CQM
CQM

Reputation: 44220

GSON from json file to object

how can I parse an object that looks like this with GSON:

{ response: 
          { value1: 0, 
            value2: "string", 
            bigjsonObject: {
                 value1b: 0,
                 bigJSONArray: [...]
          }
 }

All of the examples in GSON have less mixed value types, and the docs mention something about how this can screw up GSON deserialization but don't elaborate and still suggest that GSON can map this to an object.

My current test using gson.fromJSON(inputstream, myObject.class) returns an object with null values, so it is not mapping them.

myObject.class contains an ArrayList of type bigJSONArray

public class myObject {

        private ArrayList<bigObjectModel> bigJSONArray;

        myObject(){};
}

my assumption is that my ArrayList object doesn't have the types it is looking for, or something. But I am misunderstanding how mapping should work in this case.

Upvotes: 0

Views: 332

Answers (1)

Darussian
Darussian

Reputation: 1593

In order to parse

{ response: 
      { value1: 0, 
        value2: "string", 
        bigjsonObject: {
             value1b: 0,
             bigJSONArray: [...]
      }
}

You need the container class to be

public class myObject {

    private int    value1;
    private String value2;
    private Foo    bigjsonObject;
}

Where the Class Foo is

public class Foo {

    private int value1b;
    private ArrayList<bigObjectModel> bigJSONArray
}

You may ommit any field and GSON will just skip it

Upvotes: 1

Related Questions