2014-05-10 25 views
0

我有从服务器返回JSON对象的一个​​阵列(如下),的Android不能转换到的JSONObject错误

["{\"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\"}}}"] 

这似乎是有效的JSON按照JSONLint(http://jsonlint.com/)。但是在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(); 
    } 
} 

好像我失去了一些东西在这里,我似乎无法人物摆脱这里的问题。任何帮助表示赞赏。

回答

2

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); 
     } 
+0

荡,不能相信我没赶上。谢谢! –

相关问题