如何在iOS中解析mapquest地理编码JSON

时间:2013-07-23 21:15:12

标签: ios json parsing nsdictionary mapquest

我正在尝试解析Mapquest地理编码API的JSON结果。

NSDictionary *JSONReponseDic = [NSJSONSerialization JSONObjectWithData:mapquestdata options:0 error:&error];
NSMutableArray *resultsArray = [JSONReponseDic objectForKey:@"results"];

NSDictionary *locationDic = [resultsArray objectAtIndex:0];
NSLog(@"loc dic %@", locationDic);

NSString *city = [locationDic objectForKey:@"adminArea5"];
NSLog(@"city : %@", city);

我可以解析到locationDic,它返回

loc dic {
locations =     (
            {
        adminArea1 = US;
        adminArea1Type = Country;
        adminArea3 = CA;
        adminArea3Type = State;
        adminArea4 = "Santa Clara County";
        adminArea4Type = County;
        adminArea5 = "Los Altos";
        adminArea5Type = City;
        displayLatLng =             {
            lat = "37.37964";
            lng = "-122.11877";
        };
        dragPoint = 0;
        geocodeQuality = POINT;
        geocodeQualityCode = P1AAA;
        latLng =             {
            lat = "37.37949";
            lng = "-122.11903";
        };
        linkId = 0;
        mapUrl = "http://www.mapquestapi.com/staticmap/v4/getmap?key=Fmjtd|luub206tl1,rg=o5-9ubah0&type=map&size=225,160&pois=purple-1,37.37949,-122.11903,0,0|&center=37.37949,-122.11903&zoom=15&rand=-159915059";
        postalCode = "94022-2707";
        sideOfStreet = R;
        street = "145 1st St";
        type = s;
    }
);
providedLocation =     {
    location = "145 1st St,Los Altos, CA 94022";
};
}

然后,当我尝试获取城市名称时,日志返回null。我是以正确的方式解析这个吗?

1 个答案:

答案 0 :(得分:2)

应为[locationDic valueForKeyPath:@"locations.adminArea5"];