[英]Android cannot be converted to JSONObject error
I have a array of JSON objects being returned from a server (as follows), 我有一个从服务器返回的JSON对象数组(如下所示),
["{\"schedule\":{\"type\":\"times\"},\"videos\":{\"abc\":\"def\"}}","{\"schedule\":{\"type\":\"tod_repeat\",\"start\":\"00:09:00\"},\"videos\":{\"mk_320059\":{\"url\":\"http://m.mtvkatsomo.fi/?progId=320059\",\"siteId\":\"mk\"}}}"]
which seems to be valid json as per JSONLint ( http://jsonlint.com/ ). 根据JSONLint( http://jsonlint.com/ ),这似乎是有效的json。 However in android when I try to convert this to an object I get an exception,
但是在android中,当我尝试将其转换为对象时,我得到一个异常,
org.json.JSONException: Value {"schedule":{"type":"times"},"videos":{"abc":"def"}} at 0 of type java.lang.String cannot be converted to JSONObject
The relevant code is, 相关代码是,
if (resp != null) {
try {
JSONArray lists = new JSONArray(resp);
Log.d(CLASS_NAME, "STRING REP:"+lists.getJSONObject(0).toString()); // <-- at this line
} catch (JSONException e) {
e.printStackTrace();
}
}
It seems like I am missing something here as I cant seem to figure out what the problem is here. 好像我在这里遗漏了一些东西,因为我似乎无法弄清楚问题在这里。 Any help is appreciated.
任何帮助表示赞赏。
Root JSONArray
contain Strings as item instead of JSONObject
so try as to get JSONObject
from JSONArray
: Root
JSONArray
包含字符串作为项而不是JSONObject
因此尝试从JSONArray
获取JSONObject
:
JSONArray lists = new JSONArray(resp);
for (int i = 0; i < lists.length(); i++) {
String str_value= lists.optString(i);
// get JSONObject from String
JSONObject jsonobj=new JSONObject(str_value);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.