我在我的项目中使用了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;
例如,我得到如下的Server-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
发布于 2014-03-11 18:24:44
问题出在json字符串中的"info": {}
行。
您的类具有private String info;
字符串类型,并且在您的JSON字符串中为JSONObject。
它将尝试将JSONObject转换为字符串,这会给出错误Expected a string but was BEGIN_OBJECT
。JAVA无法将JSONObject转换为GSON
字符串。
数组group
的第一个元素中info
的值是正确的,即"info": "xyz"
,但第二个元素中相同的变量值不同。
检查info
的值,如果它是字符串,则需要检查来自服务器的JSON响应,如果不是,则需要将其类型更改为类变量。
https://stackoverflow.com/questions/22321962
复制相似问题