从iOS中的数组中获取字典值?

时间:2012-12-24 11:36:20

标签: iphone ios nsmutabledictionary

我有以下NSArray

(
        {
        geometry =         {
            location =             {
                lat = "37.789632";
                lng = "-122.417004";
            };
        };
        icon = "http://maps.gstatic.com/mapfiles/place_api/icons/doctor-71.png";
  vicinity = "900 Hyde Street, San Francisco";
    },
        {
        geometry =         {
            location =             {
                lat = "37.794774";
                lng = "-122.398941";
            };
        };
        icon = "http://maps.gstatic.com/mapfiles/place_api/icons/lodging-71.png";
 vicinity = "2 Embarcadero Center, San Francisco";
}
)

从这里我怎样才能从每个索引中获取latitudelongitude

2 个答案:

答案 0 :(得分:7)

你甚至没想过想出这个? 我会帮助你的乐趣,但帮助自己的男人

NSArray *values = ...;
for (NSDictionary *geometry in values) {
    NSDictionary *loc = [geometry valueForKey:@"location"];
    if (loc) {
        NSNumber *lat = [loc valueForKey:@"lat"];
        NSNumber *lng = [loc valueForKey:@"lng"];
        if (lat && lng) {
            // do whatever
        }
    }
}
编辑:也许就像这样

NSArray *values = ...;
for (NSDictionary *item in values) {
    NSDictionary *geo = [item valueForKey:@"geometry"];
    if (geo) {
        NSDictionary *loc = [geometry valueForKey:@"location"];
        if (loc) {
            NSNumber *lat = [loc valueForKey:@"lat"];
            NSNumber *lng = [loc valueForKey:@"lng"];
            if (lat && lng) {
                // do whatever
            }
        }
    }
}

答案 1 :(得分:2)

for (int i=0 ; i<[parsedAry count] ; i++) {

    NSMutableDictionary * location = [[[parsedAry objectAtIndex:i]objectForKey:@"geometry"]objectForKey:@"location"];
    NSString * lat = [location objectForKey:"lat"];
    NSString * lng = [location objectForKey:"lng"]; 
    [latAry addObject:lat];
    [LongAry addObject:lng];


}

您必须创建并分配latAry和longAry