我有一个从服务器返回的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\"}}}"]
根据JSONLint(http://jsonlint.com/)似乎是有效的json。但是在android中,当我尝试将其转换为对象时,我得到一个例外,
org.json.JSONException: Value {"schedule":{"type":"times"},"videos":{"abc":"def"}} at 0 of type java.lang.String cannot be converted to JSONObject
相关代码是,
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();
}
}
似乎我在这里遗漏了一些东西,因为我似乎无法弄清楚这里的问题是什么。任何帮助表示赞赏。
答案 0 :(得分:2)
根JSONArray
包含字符串作为项而不是JSONObject
,因此请尝试从JSONObject
获取JSONArray
:
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);
}