2011-11-21 90 views
1

我JsonString:對象是不是有效的JSONArray

{"Data":{"pics":{"24":{"ID":"24","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/010.jpg"},"23":{"ID":"23","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/009.jpg"},"22":{"ID":"22","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/008.jpg"},"21":{"ID":"21","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/007.jpg"},"20":{"ID":"20","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/006.jpg"},"19":{"ID":"19","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/005.jpg"},"18":{"ID":"18","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/004.jpg"},"17":{"ID":"17","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/003.jpg"},"16":{"ID":"16","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/002.jpg"},"15":{"ID":"15","LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/001.jpg"},"14":{"ID":"14","LINK":"http:\/\/orf.at\/mojo\/1_1\/storyserver\/\/news\/images\/logo.newsorfon.png"}}},"Message":null,"Code":200} 

我想分析它來獲得ID和鏈接到我的樹圖。

我的代碼:

JSONObject parse = new JSONObject(msg.obj.toString()); 
       JSONObject data = parse.getJSONObject("Data"); 
       JSONArray arr = data.getJSONArray("pics"); 
       for (int i = 0; i<arr.length(); i++) { 
        JSONObject obj = arr.getJSONObject(i); 
        list.put(Integer.parseInt(obj.getString("ID")), obj.getString("LINK")); 
        Log.e("FunPics",obj.getString("ID")+ " " + obj.getString("LINK")); 
       } 

錯誤:

11-21 11:32:21.005: W/System.err(393): org.json.JSONException: JSONObject["pics"] is not a JSONArray.
11-21 11:32:21.015: W/System.err(393): at org.json.JSONObject.getJSONArray(JSONObject.java:366)

+0

把JSON字符串的JSON數組我的意思是把「msg.obj.toString()」到getJSONArray(),而不是「圖片」,然後使用循環獲取「圖片」作爲jsonobject ...!和一個「更多」的東西是來自webservice的JSONString是錯誤的,它必須以[{Data:......}]格式出現 –

回答

2

pics不是數組,它是一個對象,陣列通過在[](如指定here)是表示:

{ 
"Data":{ 
    "pics":{ 
     "24":{ 
      "ID":"24", 
      "LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/010.jpg" 
     }, 
     "23":{ 
      "ID":"23", 
      "LINK":"http:\/\/www.orschlurch.net\/wp-content\/gallery\/pixxdump-144\/009.jpg" 
     }, 
    } 
}, 
.... 
"Message":null, 
"Code":200 
} 
1

「pics」是一個json對象不是jsonArray所以寫data.getJSONObject("pics")代替data.getJSONArray("pics");

1

pics鍵有jsonObject作爲值不是jsonArray。

JSONObject picsJson = data.getJSONObject("pics");

你必須通過所有的按鍵進行迭代,並得到畫面的每個信息。使用鑰匙迭代器,點擊下方的鏈接,

http://developer.android.com/reference/org/json/JSONObject.html

Iterator keys = picsJson.keys(); 
while(keys.hasNext()) { 
相關問題