2014-05-11 50 views
0

我試圖解析這段代碼:我需要「響應」下的「文檔」下的「名稱」屬性。在java中解析JSON代碼Android

{ 
    "responseHeader": { 
    "status": 0, 
    "QTime": 1, 
    "params": { 
     "q": "Lecture1-2", 
     "_": "1399736575991", 
     "wt": "json" 
    } 
    }, 
    "response": { 
    "numFound": 6, 
    "start": 0, 
    "docs": [ 
     { 
     "id": "TWINX2048-3200PRO", 
     "name": "CORSAIR XMS 2GB (2 x 1GB) 184-Pin DDR SDRAM Unbuffered DDR 400 (PC 3200) Dual Channel Kit System Memory - Retail", 
     "manu": "Corsair Microsystems Inc.", 
     "manu_id_s": "corsair", 
     "cat": [ 
      "electronics", 
      "memory" 
     ], 
     "features": [ 
      "CAS latency 2,\t2-3-3-6 timing, 2.75v, unbuffered, heat-spreader" 
     ]}} 

我試圖做到這一點,如:

JSONObject nodeRoot = new JSONObject(result); 
       JSONObject nodeStats = nodeRoot.getJSONObject("response"); 
       String sSDR = nodeStats.getString("docs"); 

但I'having其他結果......

回答

1

"docs": [JSONArray

你有什麼

String sSDR = nodeStats.getString("docs"); 

更改爲

JSONObject nodeStats = nodeRoot.getJSONObject("response"); 
JSONArray jr = (JSONArray) nodeStats.getJSONArray("docs"); 
for(int i=0;i<jr.length();i++) 
{ 
JSONObject jb = jr.getJSONObject(i); 
String id = jb.getString("id"); 
} 
+0

感謝它的工作原理:)我很困惑陣列之間進行區分,並且反對 –

+0

@MedAmini'['代表JSONArray節點和''{代表JSONObject節點 – Raghunandan

+0

是@Raghunandan謝謝! –