配列内に配列を持つ単純なjson形式がありますが、内部配列を取得する方法がわかりません。NSArray
「通勤」タグをまたはとして取得するにはどうすればよい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)];
}