我的json stiring是:
{
"locations" :[
{
id = 0;
lat = "41.653048";
long = "-0.880677";
name = "LIMPIA";
},
{
id = 1;
lat = "41.653048";
long = "-0.890677";
name = "LIMPIA2";
}
]
}
使用:
NSDictionary * root = [datos_string1 JSONValue];
NSArray *bares = (NSArray *) [root objectForKey:@"locations"];
for (NSArray * row in bares) {
NSString *barName1 = [bares valueForKey:@"name"];
NSLog(@"%@",barName1);
}
我从NSlog获得两次输出 ( LIMPIA, LIMPIA2 )
所以这是错的。我需要为每个项目抽取di单值参数(lat,lon和nombre)(以便在mapkit应用程序中使用)。你可以帮帮我吗?
答案 0 :(得分:1)
我需要为每个项目抽取单个值参数(lat,lon和nombre)(以便在mapkit应用程序中使用)
如果我正确理解了您的问题,那么您正试图访问locations
数组中每个字典中的每个值,是吗?
为了访问每个值(如果这确实是你的问题),这应该有效:
NSDictionary *root = [datos_string1 JSONValue];
NSArray *bares = (NSArray *)[root objectForKey:@"locations"];
// Each item in the array is a dictionary, not an NSArray
for (NSDictionary *dict in bares) {
// Loop over keys
for (NSString *key in [dict allKeys]) {
NSLog(@"dict[%@] == %@", key, [dict objectForKey:key]);
}
}