【发布时间】:2014-03-11 09:48:10
【问题描述】:
我在我的项目中使用 Gson。但它返回我错误
String sig = PortalConfig.getSignature(method, callId, params);
String url = PortalConfig.getUrl(method, callId, sig, params);
String plainResponse = BaseClientCommunicator.executeGetMethod(url);
GsonBuilder builder = new GsonBuilder();
Gson gsonObject = builder.create();
response = gsonObject.fromJson(plainResponse, GetMenuResponse.class);
return response;
例如我得到这样的服务器响应
{
"group":
[
{
"id": "206896",
"name": "Ryż",
"info": "xyz"
},
{
"id": "206897",
"name": "Buraki",
"info": {}
}
]
}
我有错误期望一个字符串但是 BEGIN_OBJECT
Exception in thread "main" com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 16151
我应该如何处理这个异常??
public class GetMenuResponse
{
@SerializedName("group")
private group[] group;
//method get and set
//to string method
}
public class group
{
@SerializedName("id")
private String id;
@SerializedName("name")
private String name;
@SerializedName("info")
private String info;
//method get and set
//to string method
}
我无权访问数据库,因为我使用 API
【问题讨论】:
-
我们必须查看您的
GetMenuResponse课程才能找出到底出了什么问题。 -
数组中第二项的“info”字段的类型为object,但第一项的类型为string。找出为什么它在生产端的序列化方式不同。
-
你的 json 无效去检查自己jsonlint.com
-
现在是正确的,这只是我的问题的一个例子,我不知道如何处理这个错误
-
查看 Yagnesh 的回答,并在 @SerializedName("@id") 等中删除 id 之前的 '@'。