javax.ejb.EJBException:com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_OBJECT但是BEGIN_ARRAY

时间:2014-02-03 05:35:19

标签: json google-maps-api-3 gson

GSON抛出语法异常将JSON解析为Java对象时。在这里,我附加了我的JSON和解析JSON的类以及解析JSON值的代码。请帮我修复此错误。

以下是我要解析的JSON响应。

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":{}}
           }
   }
]

Above Json已被以下JAVA类解析。在下面的类结构中,我正在犯错误。请指导我在哪里犯错误。

**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 - 将JSON更改为JAVA对象

    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 个答案:

答案 0 :(得分:1)

请将您的数据类更改为:

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

}

并将b类更改为:

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


}

使用:

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

你的形状类将被填充。

答案 1 :(得分:1)

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

最后将其解析为

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

如果你将这样解析,你会得到同样的错误:预期BEGIN_OBJECT但是BEGIN_ARRAY

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