【问题标题】:GSON - JsonSyntaxException - Expected name at line 7 column 4GSON - JsonSyntaxException - 第 7 行第 4 列的预期名称
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 我不明白。我那里没有任何额外的开口花括号。
  • 所以在configuration.json里面有两个对象,逗号分隔
  • 对不起,我以为是对象中的对象,我改变了答案。对于那个尾随的逗号,你有
  • Exception com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: 使用 JsonReader.setLenient(true) 在第 7 行第 3 列 com.google.gson.JsonSyntaxException 接受格式错误的 JSON : com.google.gson.stream.MalformedJsonException: 使用 JsonReader.setLenient(true) 在第 7 行第 3 列接受格式错误的 JSON
【解决方案2】:

使用

catch(JsonSyntaxException e)

而不是

catch(MalformedJsonException e)

因为 MalformedJsonException 是一些内部异常,而 JsonSyntaxException 是实际抛出的异常。这是一个代码sn-p

            String response="Something";
            JsonElement my_json;
            try {
                my_json=jsonParser.parse(response);
            } catch(JsonSyntaxException e) {
                e.printStackTrace();
                JsonReader reader = new JsonReader(new StringReader(response));
                reader.setLenient(true);
                my_json=jsonParser.parse(reader);
            }

【讨论】:

    【解决方案3】:

    您有两个对象,但从 GSON 获得了一个对象。要么你应该使用下面的 JSON 字符串格式从这个 JSON 字符串中获取 Result 对象。

    {
        "objectid" : "test",
        "dtype" : "test",
        "type" : "test",
        "name" : "test",
        "description" : "test" 
    

    }

    第二个选项:-

    您将不得不更改 JSON 字符串格式。您必须在 JSON 数组中更改它并获取该对象的 ArrayList,然后,我们可以使用数组列表索引获取 Result 对象。新的 JSON 字符串将是这样的。

        {"resultArray":[{
        "objectid" : "test",
        "dtype" : "test",
        "type" : "test",
        "name" : "test",
        "description" : "test" 
               },
          {
        "objectid" : "test",
        "dtype" : "test",
        "type" : "test",
        "name" : "test",
        "description" : "test"
            }]}
    

    以及获取结果对象的 java 代码。

    Gson g = new Gson();
    ResultList r = g.fromJson(res.toString(), ResultList.class);
    ArrayList<Result> resultList = r.getResultList();
           for(int x=0 ;x<resultList.size();x++){
                  Result result = resultList.get(x);
                  }
    

    ResultList 模型是:-

    public class ResultList {
    @SerializedName("resultArray")
    public ArrayList<Result> resultList;
    public getResultList(){
              return resultList;
               } 
           }
    

    @Note: - Result.java 的使用与上面给出的相同。

    【讨论】:

      【解决方案4】:

      我的问题是 JSON 字符串太长,解析器无法处理。我缩短了字符串并且它起作用了。

      【讨论】:

        猜你喜欢
        • 2023-04-09
        • 2021-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-10-04
        • 1970-01-01
        • 1970-01-01
        • 2017-02-15
        相关资源
        最近更新 更多