2012-05-17 48 views
0

可能重複:
Sending and Parsing JSON in Android我們如何解析這個json數據?

{ 
    "title": " '''Sachin Ramesh Tendulkar''' (born 24 April 1973) is an Indian cricketer widely regarded as one of the greatest batsmen in the history of cricket. ", 
    "sub": { 
     "sub0": { 
      "name": "Cricket", 
      "importance": "1" 
     }, 
     "sub1": { 
      "name": "Wisden Cricketers of the Year", 
      "importance": "1" 
     }, 
     "sub2": { 
      "name": "Marathi people", 
      "importance": "1" 
     }, 

    }, 
    "heading": { 
     "p1": { 
      "point": " Tendulkar . " 
     }, 
     "p2": { 
      "point": " He." 
     }, 
     "p3": { 
      "point": " 2009. " 
     }, 
    } 
} 
+0

先找到任何JSON解析教程。 – Shaiful

+0

結帳傑克遜http://jackson.codehaus.org/ – dira

回答

2

每當從網絡獲取的結果返回你的字符串,你必須把它轉換爲JSONObject的這樣

JSONObject json = new JSONObject (response); 

通過這你會得到整個字符串轉換爲JSON然後你可以R使用的JSONObject類方法是這樣

String player name = json.getString("title"); 

從JSON etrive所有值,你會得到這樣的結果

薩欽拉梅什德家「」」(生於1973年4月24日)是印度板球廣泛認爲是最大的一個板球史上的擊球手。

+0

此代碼不準確,其JSONObject不JsonObject –

+0

@Ian感謝您糾正我 –

1

在這種情況下,您可以使用JSONObject類的keys方法。它基本上會返回鍵,然後你可以重複獲得,並把該值在地圖的Iterator:

try { 
     JSONObject jsonObject = new JSONObject(theJsonString); 
     Iterator keys = jsonObject.keys(); 
     Map<String, String> map = new HashMap<String, String>(); 
     while (keys.hasNext()) { 
      String key = (String) keys.next(); 
      map.put(key, jsonObject.getString(key)); 
     } 
     System.out.println(map);// this map will contain your json stuff 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

參見:Parsing Json StringParse Json String in Android