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

2024-05-02

我有以下结果类,其对象将以 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 响应返回。如下:

{
        "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。

My code:

Gson g = new Gson();
Result r = g.fromJson(res.toString(), Result.class);

where 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

有什么指点吗?


如果这是实际的 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 你想要一个结果数组 返回

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

GSON - JsonSyntaxException - 第 7 行第 4 列的预期名称 的相关文章

随机推荐