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.
jsonDatayou're receiving before you parse it?