-4

以下 JSON 提要包含订单。

当每个订单不包含密钥(不能使用 objectForKey)时,如何检索每个订单?在左括号和第一个大括号之间没有键。

[ <-----
    { <-----
        "person": {
            "address": "My road",
            "city": "My City",
            "name": "My Name",
        },
        "orderDate": "30.05.2012 10:27",
        "orderId": 10001,
        "orderlines": [
            {
                "unit": {
                    "unitId": "RA005",
                    "name": "My Unit name",
                },
                "id": 10007,
                "deliveryInfo": {
                    "count": 1,
                    "date": "30.05.2012",
                    "format": "MY_FORMAT",
                },
                "sum": 0,
                "details": "Testing",
                "priority": 3,
            }
        ],
        "received": true,
        "status": "REGISTERED",
        "sumPrice": 0
    },
    {
        "person": {
            "address": "My road 2",
            "city": "My City 2",
            "name": "My Name 2",
        },
        "orderDate": "30.04.2012 10:27",
        "orderId": 10002,
        "orderlines": [
            {
                "unit": {
                    "unitId": "RA006",
                    "name": "My Unit name 2",
                },
                "id": 10008,
                "deliveryInfo": {
                    "count": 2,
                    "date": "01.06.2012",
                    "format": "MY_FORMAT",
                },
                "sum": 0,
                "details": "Testing",
                "priority": 2,
            }
        ],
        "received": true,
        "status": "REGISTERED",
        "sumPrice": 0
    }
]
4

2 回答 2

1

每当你看到 [] (方括号),它代表数组。所以使用索引处的对象来返回索引处。一种方法是将对象传递到数组中

NSMutableArray *arr = (NSMutableArray*) jsonData;
于 2012-05-30T11:38:36.177 回答
1

因为你的 json 的根是一个数组,

NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options: NSJSONReadingMutableContainers error: &e];

if (!jsonArray) {
  NSLog(@"Error parsing JSON: %@", e);
} else {
   for(NSDictionary *item in jsonArray) {
      NSLog(@"Item: %@", item);
   }
}
于 2012-05-30T11:40:22.737 回答