2013-07-23 46 views
1

我想解析Mapquest地理編碼API的JSON結果。如何解析iOS中的mapquest地理編碼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。我解析這個正確的方式?

回答

2

它應該是[locationDic valueForKeyPath:@"locations.adminArea5"];

+0

我做了'NSString * city = [locationDic objectForKey:@「locations.adminArea5」];'它仍然返回NULL' – Spenciefy

+0

請參閱我編輯的答案。我從內存中寫入,正確的方法是'valueForKeyPath:'而不是'objectForKey:'。 –

+0

謝謝。 valuekeyforpath似乎做的工作。 – Spenciefy