我有一个简单的 json 格式,数组中有一个数组,但我不知道如何获取内部数组。如何将“通勤”标签作为 anNSArray
或 a获取NSDictionary
?
这是我的json:
{
"Language": "EN",
"Place": [
{
"City": "Stockholm",
"Name": "Slussen",
"Commute": [
"Subway",
"Bus"
]
},
{
"City": "Gothenburg",
"Name": "Central station",
"Commute": [
"Train",
"Bus"
]
}
]
}
这是我的代码:
NSString *textPath = [[NSBundle mainBundle] pathForResource:@"Places" ofType:@"json"];
NSError *error;
NSString *content = [NSString stringWithContentsOfFile:textPath encoding:NSUTF8StringEncoding error:&error]; //error checking omitted
NSData *jsonData = [content dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary* json = [NSJSONSerialization
JSONObjectWithData:jsonData
options:kNilOptions
error:&error];
NSArray* AllPlaces = [json objectForKey:@"Place"];
for(int n = 0; n < [AllPlaces count]; n++)
{
PlaceItem* item = [[PlaceItem alloc]init];
NSDictionary* place = [AllPlaces objectAtIndex:n];
item.City = [place objectForKey:@"City"];
item.Name = [place objectForKey:@"Name"];
NSDictionary* commutes = [json objectForKey:@"Commute"];
[self.placeArray addObject:(item)];
}