0

我正在解析一些在数组中包含数组的 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"
      },

我可以很好地获取“shipments”数组的内容,但我不知道如何获取“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());
        }

我如何对“items”数组执行与“shipments”数组相同的操作?

4

3 回答 3

1

你必须items从 Shipment 数组中获取数组,就像你做货物一样,然后遍历它,就像你做货物一样。

它可能看起来像:

 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());
        }
于 2013-05-28T00:21:23.810 回答
0

items是一个位于数组内部的JSONshipments数组,因此您需要在 中获取items数组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"));
            //iterate over items
        }

希望这有帮助,祝你好运

于 2013-05-28T00:27:54.467 回答
0

试试下面的代码:

JSONObject jObject = new JSONObject(yourJSONString);
JSONObject trackInfo = jObject.getJSONObject("TrackingInformationResponse");
JSONArray shipMents = trackInfo.getJSONArray("shipments");
JSONArray items = shipMents.getJSONArray("items");
于 2013-05-28T01:18:09.157 回答