GSON-JsonSyntaxException-第7行第4列的预期名称 [英] GSON - JsonSyntaxException - Expected name at line 7 column 4

查看:134
本文介绍了GSON-JsonSyntaxException-第7行第4列的预期名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下结果类,其对象将作为JSON返回.

I have the following result class whose object is to be returned as 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,并以json的形式返回为HTTP RESPONSE.如下:

I have a configuration json which is read my the main .java and returned as json as HTTP RESPONSE. It is as below:

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

Using Gson, it reads the configuration.json file and has to return a json.

我的代码:

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

其中res.toString()以字符串的形式获取configuration.json文件的内容.

where res.toString() gets me the configuration.json file content as string.

我的问题:

我遇到以下异常:

Exception com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:使用JsonReader.setLenient(true)在第7行第3栏第3栏接受格式错误的JSON.
com.google.gson.JsonSyntaxException:com.google.gson.stream.MalformedJsonException:使用JsonReader.setLenient(true)在第7行第3栏接受格式错误的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语法错误.因此,这可能是最先要看的地方之一.

If this is the actual json: You have an extra comma here and a spelling error. The error says you have bad json syntax. So this is probably one of the first places to look.

{
            "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您想要一个结果数组返回

You also seem to be parsing two objects and telling gson you want one result object from it. Consider either parsing the objects separately or tell gson you want a result array Back

这篇关于GSON-JsonSyntaxException-第7行第4列的预期名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆