Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Json异常类型错误_Java_Json_Eclipse_Gson - Fatal编程技术网

Java Json异常类型错误

Java Json异常类型错误,java,json,eclipse,gson,Java,Json,Eclipse,Gson,我在我的项目中使用Gson。但它返回给我一个错误 String sig = PortalConfig.getSignature(method, callId, params); String url = PortalConfig.getUrl(method, callId, sig, params); String plainResponse = BaseClientCommunicator.executeGetMethod(url); GsonBuilder builder = new G

我在我的项目中使用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\u对象

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

问题在于json字符串中的第
行“info”:{}

你的类有
私有字符串信息字符串类型,在JSON字符串中是JSONObject。
它将尝试将JSONObject转换为字符串,这将导致错误
预期为字符串,但被BEGIN\u OBJECT
GSON
API无法将JSONObject转换为JAVA字符串

数组
的第一个元素中
info的值是正确的,即
“info”:“xyz”
,但第二个元素中相同的变量值不同。

检查
info
的值,如果它是字符串,则需要检查来自服务器的JSON响应,如果不是字符串,则需要将其类型更改为类变量。

我们必须查看您的
GetMenuResponse
类才能找出到底哪里出错了。数组中第二项的“info”字段具有type object,但是第一个类型是string。找出它在生产端以不同方式序列化的原因。您的json无效,现在请检查您自己是否正确,这只是我的问题的一个示例,我不知道如何处理此错误请参阅Yagnesh的答案,并删除@SerializedName(“@id”)和其他文件中id之前的“@”。我知道,但如何修复此错误?私有的info@user3405186首先确定json响应是info是字符串还是json对象。这是我的问题,info是空对象和字符串:/n不能同时包含这两个,你必须决定字符串还是对象。。!!然后确保从服务器收到响应时,
info
的值始终为字符串。