我解析JSON字符串并面对错误
org.json.JSONException:字符25处未终止的对象
我的JSON是从Facebook检索的
{Response: responseCode: 200, graphObject: GraphObject{graphObjectClass=GraphObject, state={"data":[{"venue":{"id":108242939200809,"zip":"","longitude":11.4,"latitude":62.5833,"street":""},"location":"Røros, Norway","eid":1473462312875161,"pic_big":"https:\/\/fbcdn-profile-a.akamaihd.net\/static-ak\/rsrc.php\/v2\/yn\/r\/5uwzdFmIMKQ.png","pic_small":"https:\/\/fbcdn-profile-a.akamaihd.net\/static-ak\/rsrc.php\/v2\/yy\/r\/XcB-JGXohjk.png","description":"Test","name":"Test"},{"venue":{"id":105818232792451,"zip":"","longitude":108.217,"latitude":16.0167,"street":""},"location":"Hòa Vang","eid":1425682134338854,"pic_big":"https:\/\/fbcdn-profile-a.akamaihd.net\/static-ak\/rsrc.php\/v2\/yn\/r\/5uwzdFmIMKQ.png","pic_small":"https:\/\/fbcdn-profile-a.akamaihd.net\/static-ak\/rsrc.php\/v2\/yy\/r\/XcB-JGXohjk.png","description":"test","name":"Test"}]}}, error: null, isFromCache:false}
我的JSON解析器是
public static JSONArray parse(Response response) throws JSONException{
JSONArray jsonArray=new JSONArray(response.toString());
return jsonArray;
}
请帮帮我。非常感谢你。
答案 0 :(得分:3)
我得到同样的错误&解决它们像:
GraphResponse响应;
<强> 之前: 强>
JSONObject jObjResponse = new JSONObject(String.valueOf(response.getJSONObject()));
<强> 后: 强>
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>notepadplusplus</id>
<version>6.8.5</version>
<title>Notepad++</title>
<authors>Notepad ppl</authors>
<owners>King Kong</owners>
<projectUrl>https://notepad-plus-plus.org/</projectUrl>
<iconUrl>http://i1-win.softpedia-static.com/screenshots/icon-60/Notepad-plus-plus.png</iconUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Notepad++ is a free (as in "free speech" and also as in "free beer") source code editor and Notepad replacement that supports several languages. Running in the MS Windows environment, its use is governed by GPL License.</description>
<summary>Notepad++</summary>
<tags>notepad notepad++ texteditor text editor</tags>
</metadata>
<files>
<file src="tools\chocolateyInstall.ps1" target="tools\chocolateyInstall.ps1" />
<file src="tools\chocolateyUninstall.ps1" target="tools\chocolateyUninstall.ps1" />
</files>
</package>
答案 1 :(得分:3)
只需使用JSONObject graphObj =response.getJSONObject();
即可获得graphObject
。
答案 2 :(得分:2)
您的JSON无效。需要引用所有变量名称。
答案 3 :(得分:2)
这可能是因为您试图通过response
将response
转换为String之后解析response.toString()
因此,如果您的response
是
{"title":"This is Title","message":"This is message"}
,如果您通过String
将其转换为response.toString()
比您的response
会这样
{title:This is Title,message:This is message}
,并且您尝试解析类型为response
的{{1}},则编译器将无法解析该String
,它将response
throws
像{ {1}}
因此,请确保您正在解析有效的JSON。
答案 4 :(得分:0)
更改
JSONArray jsonArray=new JSONArray(response.toString());
到
JSONObject start = new JSONObject(String.valueOf(response.getJSONObject()));
然后直接打电话
JSONArray data = start.getJSONArray("data");
它为您提供了数据&#34; JSON数组。
答案 5 :(得分:0)
try {
JSONObject jsonData = response.getJSONObject();
JSONArray jsonArray = jsonData.getJSONArray("data");
Log.e("MainActivity ", "jsondata --> " + jsonArray.toString(2));
Log.e("MainActivity ", "jsonArray.length()-> " + jsonArray.length());
likesPojos.clear();
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject json = jsonArray.getJSONObject(i);
Log.e("MainActivity ", "message --> " + json.getString("name"));
Log.e("MainActivity ", "id --> " + json.getString("id"));
LikesPojo likesPojo = new LikesPojo();
likesPojo.setId(json.getString("id"));
likesPojo.setName(json.getString("name"));
likesPojos.add(likesPojo);
}
} catch (Exception e) {
e.printStackTrace();
}