Nam*_*bie 11 java rest json jax-rs gson
我有以下结果类,其对象将作为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;
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个配置json,它读取我的主.java并作为HTTP RESPONSE返回json.如下:
{
"objectid" : "test",
"dtype" : "test",
"type" : "test",
"name" : "test",
"description" : "test" // removed
},
{
"objectid" : "test",
"dtype" : "test",
"type" : "test",
"name" : "test",
"description" : "test"
}
Run Code Online (Sandbox Code Playgroud)
主要.java
使用Gson,它读取configuration.json文件并返回一个json.
我的代码:
Gson g = new Gson();
Result r = g.fromJson(res.toString(), Result.class);
Run Code Online (Sandbox Code Playgroud)
在哪里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
有什么指针吗?
CBI*_*III 18
如果这是实际的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"
}
Run Code Online (Sandbox Code Playgroud)
您似乎也在解析两个对象,并告诉gson您需要一个结果对象.考虑分别解析对象或告诉gson你想要一个结果数组返回
| 归档时间: |
|
| 查看次数: |
37671 次 |
| 最近记录: |