我正在使用Gson API 2.4.2来解析json数据。请在下面找到我的json数据
var data = {
"timezoneId" : timezoneId,
"companyId" : companyId,
"testId" : testId,
"graphType" : graphType,
"locationId":locationId
};
这里timezoneId,companyId等的值是从java脚本文件中动态生成的,这就是为什么它们不是硬编码的,我从浏览器中检查过值是否正常。现在,在后台我有一个JSF bean类,如下所示: / p>
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;
}
}
这是我将json转换为java对象的代码
public void deserializeObjectCollection(String jsonData){
ResponseInfoForJSON[] responseInfoForJSON=new Gson().fromJson(jsonData, ResponseInfoForJSON[].class);
}
转换期间我收到以下错误
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:]
我在stackoverflow中已经阅读了关于此主题的其他帖子,但无法弄清楚我的问题。如果我将反序列化代码更改为以下内容:
ResponseInfoForJSON responseInfoForJSON=new Gson().fromJson(jsonData, ResponseInfoForJSON.class);
然后我收到以下错误:
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:]
任何人都可以提供任何解决方案吗??? 提前谢谢。
答案 0 :(得分:1)
我找到了解决方案
var strHelpData = JSON.stringify(data);
$("#helpJsonData").val(strHelpData);
我们需要调用JSON.stringify()方法并传递JSON数据作为参数,以String格式转换json数据,现在从服务器端我们可以将这个json数据转换为类似的java类,并使用以下代码
responseInfoForJSON = new Gson().fromJson(jsonData, ResponseInfoForJSON.class);
答案 1 :(得分:0)
Gson gson = new Gson();
JsonParser parser = new JsonParser();
JsonObject jsonObj = parser.parse(jsonData).getAsJsonObject();
ResponseInfoForJSON responseInfoForJSON = gson.fromJson( jsonObj , ResponseInfoForJSON.class);
首先,您需要将Json String解析为JsonObject
,然后您可以使用gson.fromJson()
函数。