【发布时间】:2013-07-25 14:55:22
【问题描述】:
我有以下结果类,其对象将作为 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 并作为 json 作为 HTTP RESPONSE 返回。如下:
{
"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 文件内容。
我的问题:
我遇到以下异常:
异常 com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:使用 JsonReader.setLenient(true) 在第 7 行第 3 列接受格式错误的 JSON
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: 使用 JsonReader.setLenient(true) 在第 7 行第 3 列接受格式错误的 JSON
任何指针?
【问题讨论】:
-
为什么“name”中有空格?你的错误说你有错误的 JSON
-
@ClydeByrdIII 我修好了,对不起。仍然是确切的问题。
-
您是否尝试从该 Json 解析多个
Results?如果是这样,您需要让您的 Json 看起来像一个列表,并告诉 Gson 它也是一个列表。 -
@DanW 不,只有一个结果。
-
如果这是一个结果,你不应该同时解析两个对象
标签: java json rest jax-rs gson