0

I am parsing some JSON that has arrays within arrays, and I just cant seem to get the data of the arrays within the first array.

My JSON looks like this (I cut it off in the end so it wasn't that long):

{"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"
      },

I can get the content of the "shipments" array just fine, but I have no idea how to get the contents of the "items" array. My code looks like this:

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());
        }

How would I do the same with the "items" array as I did with the "shipments" array?

1
  • the JSON is not complete! Commented May 28, 2013 at 0:16

3 Answers 3

1

You have to get the items array from inside the Shipment array, like you did the shipments, then iterate through that, like you did the shipments.

It might look something like:

 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());
        }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, this works perfectly. But is there no other way of doing this? I have a lot of nested arrays, and if I have to make a for-loop for each, it gets quite messy.
You could put the JSON into a hashmap and iterate over the hashmap using a while loop; but, you're still going to have to iterate over the set to get all the values out of each array object. You could also create a recursive function to pull out the nested arrays, but, again, you're still going to have to iterate over the array.
0

items is a JSON Array located inside the shipments array, so you need to get the items array within the shipments, maybe like this :

        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
        }

Hope this helps, Good luck

Comments

0

Try bellow code:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.