【发布时间】:2017-02-27 08:45:10
【问题描述】:
问题是关于 JsonElement。这就是 Json 中键的值。因此允许使用字符串。好像没有空格
为什么下面一行会出现 JSON 语法异常?
new JsonParser().parse("Nexus 6")
异常堆栈跟踪:
com.google.gson.JsonSyntaxException: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 8 path $
at com.google.gson.JsonParser.parse(JsonParser.java:65)
at com.google.gson.JsonParser.parse(JsonParser.java:45)
更新:
我感兴趣的是将字符串转换为 JsonElement
更新:
为什么这行得通?
new JsonParser().parse("Blah");
更新 3:
这也有效:
new JsonParser().parse("Nexus_6")
【问题讨论】:
-
“Nexus 6”不是有效的 JSON。
-
使用 JsonReader.setLenient(true) 接受格式错误的 JSON
-
@MuhammadSaqlain:它是一个字符串。为什么无效?
-
如果你看一下:tools.ietf.org/html/rfc7159 under 13. 有一个与 OP 类似的例子,我认为它实际上是有效的 JSON
-
Nexus 6不是有效的 JSON 字符串,因为它不符合 JSON 的语法。见json.org