简体   繁体   中英

Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2

I am using Gson API 2.4.2 for parsing json data.Please find below my json data

var data = {
            "timezoneId" : timezoneId,
            "companyId" : companyId,
            "testId" : testId,
            "graphType" : graphType,
            "locationId":locationId
           };

here values of timezoneId,companyId etc are dynamically generated from java script file thats why they are not hardcoded and i have inspect from my browser that values are comming properly.Now,In backed I have a JSF bean class as follows:

    package com.edfx.warm.bean;
    import java.io.Serializable;

    public class ResponseInfoForJSON implements Serializable {

      private static final long serialVersionUID = -40538895168761693L;
      private String timezoneId;
      private String companyId;
      private String testId;
      private String graphType;
      private String locationId;

      public String getTimezoneId() {
        return timezoneId;
      }

      public void setTimezoneId(String timezoneId) {
        this.timezoneId = timezoneId;
      }

      public String getCompanyId() {
        return companyId;
      }

      public void setCompanyId(String companyId) {
        this.companyId = companyId;
      }

      public String getTestId() {
        return testId;
      }

      public void setTestId(String testId) {
        this.testId = testId;
      }

      public String getGraphType() {
        return graphType;
      }

      public void setGraphType(String graphType) {
        this.graphType = graphType;
      }

      public String getLocationId() {
        return locationId;
      }

      public void setLocationId(String locationId) {
        this.locationId = locationId;
      }

      public static long getSerialversionuid() {
        return serialVersionUID;
      }
      }

here is my code for converting json to java object

       public void deserializeObjectCollection(String jsonData){
            ResponseInfoForJSON[] responseInfoForJSON=new Gson().fromJson(jsonData, ResponseInfoForJSON[].class);
       }

during conversion I am getting the following error

        Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 2
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveType AdapterFactory.java:176) [gson-2.2.4.jar:]
        at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40) [gson-2.2.4.jar:]
        at com.google.gson.internal.bind.ArrayTypeAdapter.read(ArrayTypeAdapter.java:72) [gson-2.2.4.jar:]
        at com.google.gson.Gson.fromJson(Gson.java:803) [gson-2.2.4.jar:]
        at com.google.gson.Gson.fromJson(Gson.java:768) [gson-2.2.4.jar:]
        at com.google.gson.Gson.fromJson(Gson.java:717) [gson-2.2.4.jar:]
        at com.google.gson.Gson.fromJson(Gson.java:689) [gson-2.2.4.jar:]
        at com.edfx.warm.bean.ChartBean.deserializeObjectCollection(ChartBean.java:83) [classes:]

I have gone through the other post on this topic in stackoverflow but unable to figure out my problem.If I change my deserialization code to following:

        ResponseInfoForJSON responseInfoForJSON=new Gson().fromJson(jsonData, ResponseInfoForJSON.class);

Then I got the following error:

        java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2
        at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:374) [gson-2.2.4.jar:]
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:165) [gson-2.2.4.jar:]

Can anyone provide any solution to this??? Thanks in advance.

I found the solution

var strHelpData = JSON.stringify(data);
$("#helpJsonData").val(strHelpData);

We need to call the JSON.stringify() method and pass the JSON data as parameter,to convert the json data in String format,now from serverside we can convert this json data to its analogous java class with follwing piece of code

responseInfoForJSON = new Gson().fromJson(jsonData, ResponseInfoForJSON.class);
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonObject jsonObj = parser.parse(jsonData).getAsJsonObject();
ResponseInfoForJSON responseInfoForJSON = gson.fromJson( jsonObj , ResponseInfoForJSON.class);

First you need to parse Json String into JsonObject and then you can use gson.fromJson() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM