-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

[] (角かっこ) が表示されるときはいつでも、それは配列を表します。そのため、object at index を使用して at インデックスを返します。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 に答える