我有以下结果类,其对象将作为 JSON 返回。
public class Result {
public String objectid;
public String dtype;
public String type;
public String name;
public String description;
public Result() {
this.objectid = "";
this.dtype= "";
this.type="";
this.name= "";
this.description = "";
}
public String getObjectid() {
return objectid;
}
public void setObjectid(String s) {
this.objectid = s;
}
public String getDtype() {
return dtype;
}
public void setDtype(String s) {
this.dtype= s;
}
public String getType() {
return type;
}
public void setType(String s) {
this.type = s;
}
public String getName() {
return name;
}
public void setName(String s) {
this.name = s;
}
public String getDescription() {
return description;
}
public void setDescription(String s) {
this.description = s;
}
}
我有一个配置 json,它读取我的主要 .java 并作为 HTTP 响应作为 json 返回。如下所示:
{
"objectid" : "test",
"dtype" : "test",
"type" : "test",
"name" : "test",
"description" : "test" // removed
},
{
"objectid" : "test",
"dtype" : "test",
"type" : "test",
"name" : "test",
"description" : "test"
}
主要.java
使用 Gson,它读取 configuration.json 文件并必须返回一个 json。
我的代码:
Gson g = new Gson();
Result r = g.fromJson(res.toString(), Result.class);
其中 res.toString()
以字符串形式获取 configuration.json 文件内容。
我的问题:
我遇到以下异常:
Exception com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 7 column 3
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 7 column 3
有什么建议吗?
最佳答案
如果这是实际的 json:你在这里有一个额外的逗号和一个拼写错误。该错误表明您的 json 语法错误。所以这可能是首先要看的地方之一。
{
"objectid" : "test",
"dtype" : "test",
"type" : "test",
"name " : "test",
"description" : "test", //delete this comma
},
{
"objectid" : "test",
"dtyoe" : "test", // spelling error
"type" : "test",
"name " : "test",
"description" : "test"
}
你似乎也在解析两个对象并告诉 gson 你想要一个结果对象。 考虑要么单独解析对象,要么告诉 gson 你想要一个结果数组返回
关于java - GSON - JsonSyntaxException - 第 7 行第 4 列的预期名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17862689/