Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/388.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果存在空格,JSON将在简单字符串上崩溃_Java_Android_Json_Gson - Fatal编程技术网

Java 如果存在空格,JSON将在简单字符串上崩溃

Java 如果存在空格,JSON将在简单字符串上崩溃,java,android,json,gson,Java,Android,Json,Gson,问题是关于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 a

问题是关于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")

这里的问题是,字符串值的开头和结尾不是第7个“字符串”中所述的引号


因此类似这样的内容:
new JsonParser().parse(“\”Nexus 6\”)
应该可以工作。

“Nexus 6”不是有效的JSON。请使用JsonReader.setLenient(true)接受格式错误的JSON@MuhammadSaqlain:它是一根线。为什么无效?如果你看一下:13岁以下。有一个与OP类似的例子,我认为它实际上是有效的JSON
Nexus 6
不是有效的JSON字符串,因为它不符合JSON的语法。请参阅Just以了解记录,新的JsonPrimitive也可以工作