2013-05-28 199 views
0

我解析了一些在數組中有數組的JSON,而我似乎無法獲得第一個數組內的數組數據。嵌套的JSON數組

我的JSON看起來像這樣(我到底該削減它關閉,因此它不是很久):

{"TrackingInformationResponse": { 
"shipments": [ 
{ 
    "shipmentId": "03015035146308", 
    "uri": "\/ntt-service-rest\/api\/shipment\/03015035146308\/0", 
    "assessedNumberOfItems": 1, 
    "deliveryDate": "2013-05-13T11:47:00", 
    "estimatedTimeOfArrival": "2013-05-13T16:00:00", 
    "service": { 
    "code": "88", 
    "name": "DPD" 
    }, 
    "consignor": { 
    "name": "Webhallen Danmark ApS", 
    "address": { 
     "street1": "Elsa Brändströms Gata 52", 
     "city": "HÄGERSTEN", 
     "countryCode": "SWE", 
     "country": "Sverige", 
     "postCode": "12952" 
    } 
    }, 
    "consignee": { 
    "name": "Lene Bjerre Kontor & IT Service", 
    "address": { 
     "street1": "Lene Bjerre", 
     "street2": "Ørbækvej 8, Hoven", 
     "city": "TARM", 
     "countryCode": "???", 
     "postCode": "6880" 
    } 
    }, 
    "statusText": { 
    "header": "Forsendelsen er udleveret", 
    "body": "Forsendelsen blev leveret 13-05-2013 kl. 11:47" 
    }, 
    "status": "DELIVERED", 
    "totalWeight": { 
    "value": "0.55", 
    "unit": "kg" 
    }, 
    "totalVolume": { 
    "value": "0.005", 
    "unit": "m3" 
    }, 
    "items": [ 
    { 
     "itemId": "03015035146308", 
     "dropOffDate": "2013-05-08T17:18:00", 
     "deliveryDate": "2013-05-13T11:47:00", 
     "status": "DELIVERED", 
     "statusText": { 
     "header": "Forsendelsen er udleveret til modtageren", 
     "body": "Forsendelsen blev udleveret 13-05-2013 kl. 11:47" 
     }, 

我可以得到「出貨」陣就好了的內容,但我有不知道如何獲取「items」數組的內容。我的代碼如下所示:

try { 
      JSONObject jsonObject = new JSONObject(result); 
      JSONObject TrackingInformationResponse = new JSONObject(jsonObject.getString("TrackingInformationResponse")); 
      JSONArray shipments = new JSONArray(TrackingInformationResponse.getString("shipments")); 

      for (int i = 0; i < shipments.length(); i++) { 
       JSONObject JSONitems = shipments.getJSONObject(i);   
       String shipmentId = JSONitems.getString("shipmentId"); 

       //do stuff 
      } 
     } catch (Exception e) { 
      Log.d("ReadWeatherJSONFeedTask", e.getLocalizedMessage()); 
     } 

我怎麼會做同樣的「項目」數組作爲我的「出貨量」數組做?

+0

JSON不完整! –

回答

1

您必須從Shipment數組中獲取items數組,就像您運輸貨件一樣,然後像遍歷貨件一樣遍歷。

它看起來是這樣的:

JSONObject jsonObject = new JSONObject(result); 
      JSONObject TrackingInformationResponse = new JSONObject(jsonObject.getString("TrackingInformationResponse")); 
      JSONArray shipments = new JSONArray(TrackingInformationResponse.getString("shipments")); 

      for (int i = 0; i < shipments.length(); i++) { 
       JSONObject JSONitems = shipments.getJSONObject(i);   
       String shipmentId = JSONitems.getString("shipmentId"); 
       JSONArray items = new JSONArray(JSONitems.getString("items"); 
       //get items stuff 
       //do stuff 
      } 

     } catch (Exception e) { 
      Log.d("ReadWeatherJSONFeedTask", e.getLocalizedMessage()); 
     } 
+0

謝謝,這個作品完美。但是,除此之外沒有其他辦法嗎?我有很多嵌套數組,如果必須爲每個數組做一個for-loop,它會變得非常混亂。 – Lazze

+0

您可以將JSON放入'hashmap'並使用while循環遍歷'hashmap';但是,您仍然需要迭代該集合以獲取每個數組對象的所有值。你也可以創建一個遞歸函數來取出嵌套數組,但是,你仍然需要遍歷數組。 – BlackHatSamurai

0

items是一個JSON陣列位於shipments陣列,所以你需要在shipments內得到items陣列,也許是這樣的:

 for (int i = 0; i < shipments.length(); i++) { 
      JSONObject JSONitems = shipments.getJSONObject(i);   
      String shipmentId = JSONitems.getString("shipmentId"); 
      JSONArray items = new JSONArray(JSONitems.getString("items")); 
      //iterate over items 
     } 

希望這會有所幫助,祝你好運

0

T ry波洛碼:

JSONObject jObject = new JSONObject(yourJSONString); 
JSONObject trackInfo = jObject.getJSONObject("TrackingInformationResponse"); 
JSONArray shipMents = trackInfo.getJSONArray("shipments"); 
JSONArray items = shipMents.getJSONArray("items");