java - GSON - JsonSyntaxException - 第 7 行第 4 列的预期名称

标签 java json rest 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;
    }

}

我有一个配置 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/

相关文章:

javascript - Kendo 图表绑定(bind)到 JSON

java - java中Rest WS响应中的不同日期格式

javascript - 如何获取没有对象编号的 json 对象?

ios - 无法将 Json 解析为 NSDictionary

json - 如何使用 json.net 将额外的属性添加到序列化的 JSON 字符串中?

javascript - 我使用 Node.js 和 Express 实现 Restful API 的正确 url 路径是什么

java - 在 linux 中从 c 程序调用 java 程序

java - Android 单元测试不会使用正确的应用程序类

java - 斯坦福NLP POS Tagger(MaxentTagger)

java - 如何从 zip 文件的内容 uri 获取文件路径?