0
votes

GSON Throwing Syntax exception While parsing the JSON into a Java Objects. Here I have attached my JSON and the Classes by which JSON has been parsed and the code where I am parsing the JSON values. Please help me to fix this error.

The following is my JSON Response Which is to be parsed.

JSON

[
  { "counter":1,
      "data":{
              "b":[
                    {"d":11.080666011022274,"e":-9.84375},
                    {"d":21.36033117555945,"e":-13.18359375},
                    {"d":25.55169302685644,"e":-5.09765625},
                    {"d":20.209969075006228,"e":24.9609375},
                    {"d":6.740259027196141,"e":27.7734375},
                    {"d":19.38301389529031,"e":10.01953125}
                  ],
             "gm_accessors_":{"length":null},
             "length":6,
             "gm_bindings_":{"length":{}}
       }
   },

 { "counter":2,
      "data":{
             "b":[
                     {"d":43.76263306667474,"e":60.1171875},
                     {"d":56.310038487065135,"e":47.8125},
                     {"d":60.881999484084055,"e":78.22265625},
                     {"d":55.81939178481952,"e":96.6796875},
                     {"d":44.76961886697326,"e":99.84375},
                     {"d":55.72051189919337,"e":82.08984375},
                     {"d":40.50489156437503,"e":81.5625},
                     {"d":52.74250152629922,"e":72.0703125}
                   ],
           "gm_accessors_":{"length":null},
           "length":8,
           "gm_bindings_":{"length":{}}
           }
   }
]

The Above Json has been parsed by the following JAVA classes. In the following Class structure I am making Mistake. Please guide me where I am doing the mistake.

**Parent Class -- SHAPE**

    public class Shape {

        @SerializedName("counter")
        private Integer mCounter;

        @SerializedName("data")
        private Data mData;

        public Data getmData() {
            return mData;
        }

        public void setmData(Data mData) {
            this.mData = mData;
        }

        public Integer getCounter() {
            return mCounter;
        }

        public void setCounter(Integer counter) {
            this.mCounter = counter;
        }

    }


**CHILD CLASS -- DATA**

    public class Data {

        @SerializedName("length")
        private Integer length;

        @SerializedName("b")
        private b mCoordinates;

        public Integer getLength() {
            return length;
        }

        public void setLength(Integer length) {
            this.length = length;
        }

        public b getmCoordinates() {
            return mCoordinates;
        }

        public void setmCoordinates(b mCoordinates) {
            this.mCoordinates = mCoordinates;
        }

    }

**GRAND CHILD CLASS -- b**

    public class b {

        @SerializedName("d")
        private ArrayList<Float> lattitude;

        @SerializedName("e")
        private ArrayList<Float> longtitude;

        public ArrayList<Float> getLattitude() {
            return lattitude;
        }

        public void setLattitude(ArrayList<Float> lattitude) {
            this.lattitude = lattitude;
        }

        public ArrayList<Float> getLongtitude() {
            return longtitude;
        }

        public void setLongtitude(ArrayList<Float> longtitude) {
            this.longtitude = longtitude;
        }
    }

JSON PARSING -- CHANGING JSON AS A JAVA OBJECTS

    JsonParser parser = new JsonParser();
    JsonArray jArray = parser.parse(jsonContent).getAsJsonArray();
    System.out.println("Array :_: " + jArray);
    for(JsonElement jsonElement : jArray) {
        System.out.println("JSON_ELEMENT :_: " + jsonElement);
        Shape shape =   gson.fromJson(jsonElement, Shape.class);

        System.out.println("Counter :_: " + shape.getCounter());

    }
2
You probably are passing a list of objects as the type in gson instead pass a single object. And make the list an attribute in that single object - Rat-a-tat-a-tat Ratatouille
@Rat-a-tat-a-tatRatatouille : could you please brief it ! ! ! - ArunRaj
please give me a moment. thanks - Rat-a-tat-a-tat Ratatouille

2 Answers

1
votes

Please chnage your data class to :

public class Data {

    @SerializedName("length")
    private Integer length;

    @SerializedName("b")
    // this is where the error was thrown, 
    // it was expecting an array but only received a single object.
    private List<b> mCoordinates;

    public Integer getLength() {
        return length;
    }

    public void setLength(Integer length) {
        this.length = length;
    }

    public List<b> getmCoordinates() {
        return mCoordinates;
    }

    public void setmCoordinates(List<b> mCoordinates) {
        this.mCoordinates = mCoordinates;
    }  

}

And also change the b class to:

public class b {

    @SerializedName("d")
    private double d;

    @SerializedName("e")
    private double e;

    public double getD() {
        return d;
    }

    public void setD(double d) {
        this.d = d;
    }

    public double getE() {
        return e;
    }

    public void setE(double e) {
        this.e = e;
    }


}

use:

Gson gson = new Gson();
Shape shape  = gson.fromJson(reader/string here, Shape.class);

and your shape class will be filled.

1
votes
public class Shape {
    @SerializedName("counter")
    private Integer mCounter;
    @SerializedName("data")
    private Data mData;
// geter/setter here
}

public class Data {
@SerializedName("length")
private Integer length;
@SerializedName("b")
private List<Coordinate> coordinates;
@SerializedName("gm_accessors_")
private Accessors gmAccessors;
//getter setter here
}

public class Coordinate {
    private float d;
    private float e;
}

public class Accessors {
    private Integer length;
}

Finally Parse it as

 Shape[] shapes = gson.fromJson(jArray, Shape[].class);

If you will parse like this you will get same error : Expected BEGIN_OBJECT but was BEGIN_ARRAY

Shape shape  = gson.fromJson(jArray, Shape.class);