Reputation: 1145
With my ios app i made a request to the google places api and i get a response like this:
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "Rome",
"short_name" : "Rome",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Rome",
"short_name" : "Rome",
"types" : [ "administrative_area_level_3", "political" ]
},
{
"long_name" : "Metropolitan City of Rome",
"short_name" : "RM",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Lazio",
"short_name" : "Lazio",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Italy",
"short_name" : "IT",
"types" : [ "country", "political" ]
}
],
"adr_address" : "\u003cspan class=\"locality\"\u003eRome\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eItaly\u003c/span\u003e",
"formatted_address" : "Rome, Italy",
"geometry" : {
"location" : {
"lat" : 41.9027835,
"lng" : 12.4963655
},
"viewport" : {
"northeast" : {
"lat" : 42.0505462,
"lng" : 12.7302888
},
"southwest" : {
"lat" : 41.769596,
"lng" : 12.341707
}
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "c201ff6d6339dac3b34184b3972b232aa097ff8a",
"name" : "Rome",
"place_id" : "ChIJu46S-ZZhLxMROG5lkwZ3D7k",
"reference" : "CnRoAAAAUe_x9QmJ7kGAAAoyOwa_6vGISj0hy4mvqTJNjNl9TrXqaowyKQCEQov70GTyVidSdNd9wy0MG9UWffjSmi58YG7R3j2Fr9_RoKJCjKgcxwijojmVgFNf5p-8Ja1E53D_YnzW8R0lgtY1xMmOZvxzEBIQ1ruuMP92aFYZs-EJd0McJRoUOaLCDU0K4Dh9nag9wouUAsHqC3g",
"scope" : "GOOGLE",
"types" : [ "locality", "political" ],
"url" : "https://maps.google.com/maps/place?q=Rome,+Italy&ftid=0x132f6196f9928ebb:0xb90f770693656e38",
"vicinity" : "Rome"
},
"status" : "OK"
}
I get the address_components with this code:
NSDictionary *ResultDictionary = [body objectFromJSONString];
for (NSDictionary *q in [[ResultDictionary valueForKey:@"result"]valueForKey:@"address_components"]) {
NSArray *type=[q valueForKey:@"types"];
NSArray *long_name=[q valueForKey:@"long_name"];
NSArray *short_name=[q valueForKey:@"short_name"];
}
With the same logic i can't get the viewport and location value under the geometry object, this cause a crash because the key does not exists :
for (NSDictionary *q in [[ResultDictionary valueForKey:@"result"]valueForKey:@"geometry"] ) {
NSArray *location=[q valueForKey:@"location"];
}
It seems that the json decode extract only a string and not a nsDictionary for geometry object, what's wrong?
Upvotes: 0
Views: 608
Reputation: 3329
Add this on your code and Get your value from JSON
as per your requiremnt
NSMutableDictionary *NewDict=[[[ResultDictionary valueForKey:@"result"]valueForKey:@"geometry"] objectForKey:@"location"];
NSString *lat=[NewDict objectForKey:@"lat"];
NSString *lng =[NewDict objectForKey:@"lng"];
i hope this code is useful for you.
Upvotes: 1
Reputation: 4552
Your "geometry" node contains two dictionaries, and only one of them has the "location" node, that's why it crashes (you ask both of them to give back the value of key "location" without checking first if there is one). The answer provided will fix your issue, just drill straight to the "location" part without iterating and you'll get your values.
Considering you've already got your answer, you'll probably save yourself a few headaches by using a higher level JSON library such as this one: http://www.jsonmodel.com/
Upvotes: 0