2014-03-01 48 views
1

我得到這個從服務器的Web響應,我想分析它,並把它添加到列表具有多個陣列解析JSON對象,並添加到列表

{ 
    "Id": [ 
     "7", 
     "8", 
     "9" 
    ], 
    "Title": [ 
     "Title 1", 
     "Title 2", 
     "Title 3" 
    ], 
    "Description": [ 
     "desc1", 
     "desc2", 
     "desc3" 
    ], 
    "NewsUpdatedDateTime": [ 
     "2010-02-26T10:40:30", 
     "2010-02-27T10:40:30", 
     "2010-02-28T10:40:30" 
    ], 
    "ImageUrl": [ 
     "image1.jpg", 
     "image2.jpg", 
     "image3.jpg" 
    ], 
    "LastUpdatedTime": "2010-02-28T10:40:30", 
    "ResponseMessage": "Data retrieved successfully", 
    "ResponseCode": "1" 
} 
+5

到目前爲止您嘗試過的是什麼? – Nambi

+0

那麼是什麼問題? – Piyush

回答

0

試試這個:

//your json String 
    String jsonStr; 

    if (jsonStr != null) { 
     try { 
     JSONObject jsonObj = new JSONObject(jsonStr); 

     // Getting JSON Array node 
     JSONArray data = jsonObj.getJSONArray("ID"); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
    } 

你應該閱讀一些關於android中從json解析的教程和文檔here,herehere

0
You can implement it like this : 



    List id; 
    List title; 
    String jsonTxt = inputStream; 
    JSONObject jsonObj = new JSONObject(jsonTxt); 
    JSONArray jsonArray1 = jsonObj.optJSONArray("Id"); 
    JSONArray jsonArray2 = jsonObj.optJSONArray("Title"); 
    if (jsonArray1.length() > 0) { 
    id=new ArrayList(); 
    for (int index = 0; index < jsonArray1.length(); index++) { 
    JSONObject sellerObj = jsonArray1.optJSONObject(index); 
    id.add(sellerObj.optString("name"));//name of ids 
    } 
    } 

saee作爲標題或其他數組。

2

按照this的JSON解析...

創建示例代碼爲您的JSON數據。檢查,讓我知道是它的工作?

String jsonData = your_data; 
ArrayList<String> mListID; 
ArrayList<String> mListTitle; 

try { 
JSONObject jsonOBJ = new JSONObject(jsonData); 

// Getting JSON Array node 
JSONArray mArrayID = jsonObj.getJSONArray("ID"); 
mListID = new ArrayList<String>(); 

for(int i=0 ; i<mArrayId.length(); i++) 
{ 
    mListID.add(mArrayId.get(i)) 
} 
JSONArray mArrayTitle = jsonObj.getJSONArray("Title"); 
mListTitle = new ArrayList<String>(); 
for(int j=0 ; j<mArrayTitle.length(); j++) 
{ 
    mListTitle.add(mArrayTitle.get(j)) 
} 
} catch (JSONException e) { 
    e.printStackTrace(); 
} 
0

首先你解析你的服務器響應

json jsonResponse = new json(response); 

JSONArray id[] = jsonResponse.getJSONArray("Id"); 

JSONArray title = jsonResponse.getJSONArray("Title"); 

JSONArray description[] = jsonResponse.getJSONArray("Description"); 

JSONArray newsUpdatedDateTime[] = jsonResponse.getJSONArray("NewsUpdatedDateTime"); 

JSONArray imageUrl[] = jsonResponse.getJSONArray("ImageUrl"); 

String lastUpdateTime = jsonResponse.getString("LastUpdatedTime"); 

String responseMessage = jsonResponse.getString("ResponseMessage"); 

String responseCode = jsonResponse.getString("ResponseCode"); 

// ============================ ============================================== //

然後你可以分析你保持單一陣列:

for (int i = 0; i < id.length(); i++) { // **line 2** 
    String name = id.get(i); 
    } 

我認爲這將有助於。