Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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
Android 试图解析HttpResponse的结果时出现JSONException_Android_Json - Fatal编程技术网

Android 试图解析HttpResponse的结果时出现JSONException

Android 试图解析HttpResponse的结果时出现JSONException,android,json,Android,Json,我发现每个类似的响应都有人错误地使用JSONArray。我正在使用JSONObject HttpResponse response = httpClient.execute(httpGet); String json_string = EntityUtils.toString(response.getEntity(), "UTF-8"); updateBoundaries(new JSONObject(json_string)); System.out.println(json_string)

我发现每个类似的响应都有人错误地使用JSONArray。我正在使用JSONObject

HttpResponse response = httpClient.execute(httpGet);
String json_string = EntityUtils.toString(response.getEntity(), "UTF-8");

updateBoundaries(new JSONObject(json_string));
System.out.println(json_string);
告诉我错误

org.json.JSONException:Value{“TopLeftCorner”:{“经度”:0.0,“纬度”:100.0},“TopRightCorner”:{“经度”:100.0,“纬度”:100.0},“BottomLeftCorner”:{“经度”:0.0,“纬度”:0.0},java.lang.String类型的{“经度”:100.0,“纬度”:0}无法转换为JSONObject

然而,当我将JSON复制粘贴到JSON Lint中时,它是非常好的。当我打印出它试图解析的字符串时,它看起来像

“{\'TopLeftCorner\\:{\'Longitude\':0.0,\'Latitude\':100.0,\'TopLeftCorner\':{\'Longitude\':100.0,\'Latitude\':100.0},\'BottomRightCorner\':{\'Longitude\':100.0,\'Latitude\'0.0},\'BottomRightCorner\'

看起来它应该是可接受的JSON,但是JSONObject由于某种原因无法解析它。发生什么事了

编辑:stacktrace

W/System.err: org.json.JSONException: Value {"TopLeftCorner":{"Longitude":0.0,"Latitude":100.0},"TopRightCorner":{"Longitude":100.0,"Latitude":100.0},"BottomLeftCorner":{"Longitude":0.0,"Latitude":0.0},"BottomRightCorner":{"Longitude":100.0,"Latitude":0.0}} of type java.lang.String cannot be converted to JSONObject

W/System.err:     at org.json.JSON.typeMismatch(JSON.java:111)

W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:160)

W/System.err:     at org.json.JSONObject.<init>(JSONObject.java:173)

W/System.err:     at com.example.chan.locationapiapp.MainActivity$1$1.run(MainActivity.java:62)

W/System.err:     at java.lang.Thread.run(Thread.java:761)
W/System.err:org.json.JSONException:Value{“TopLeftCorner”:{“经度”:0.0,“纬度”:100.0},“TopRightCorner”:{“经度”:100.0,“Latitude”:100.0},“BottomLeftCorner”:{“经度”:0.0},“BottomRightCorner”:{“经度”:100.0,“纬度”:0}类型java.lang.String无法转换为JSONObject
W/System.err:at org.json.json.typeMismatch(json.java:111)
W/System.err:org.json.JSONObject(JSONObject.java:160)
W/System.err:org.json.JSONObject(JSONObject.java:173)
W/System.err:at com.example.chan.locationapiapp.MainActivity$1$1.run(MainActivity.java:62)
W/System.err:at java.lang.Thread.run(Thread.java:761)

事实证明,我所访问的服务器在字符串(“字符串”)周围产生了一对额外的引号,我必须用空字符替换所有反斜杠\然后它才能解析

toString()
'd已经是一个有效的JSON字符串。另请注意:Android上不推荐使用Apache HTTP。截击可以发出JSON请求。谢谢我试着用它来代替。我厌倦了处理JSON错误,如果Volly能帮我做的话,那会好得多。是的,虽然第一个JSON看起来是正确的,
System.out.println
不应该是登录android的方式。改用
Log.d()
方法。我会将
new JSONObject(json_string)
放入一个单独的变量中,您可以稍后解析该变量,或者让
updateBoundaries
获取string对象。错误来自调用
new JSONObject(json_string)
,因此我认为这可能是一个编码问题。我真的不想做手动字符串操作,但如果JSON一直失败,那是我最后的选择。我会坚持下去的。