2014-05-10 27 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

蕩,不能相信我沒趕上。謝謝! –

相關問題