I'm pulling some data from google maps but I can't seem to do anything with it. Here's my code:
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection {
//do something with the data!
NSError *e = nil;
//parse the json data
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: receivedData options: NSJSONReadingMutableContainers error: &e];
//get the lat and long and put it into an array
locationData = [[NSMutableArray alloc] init];
NSLog(@"%@", [jsonArray objectAtIndex:0]);
}
if I log jsonArray.count I get 2, which seems right since google will return results and status at the top level. But if I try to get object 0 it crashes. If I try to do something like this it also crashes:
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection {
//do something with the data!
NSError *e = nil;
//parse the json data
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: receivedData options: NSJSONReadingMutableContainers error: &e];
//get the lat and long and put it into an array
locationData = [[NSMutableArray alloc] init];
for(NSDictionary* thisLocationDict in jsonArray) {
NSString* location = [thisLocationDict objectForKey:@"results"];
[locationData addObject:location];
}
}
I use this code to get data from Twitter with no problems. The error I get in the console is I am trying to get an object of a string:
-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x798a270
Here's the json google is sending me:
results = ( { "address_components" = ( { "long_name" = 2900; "short_name" = 2900; types = ( "street_number" ); }, { "long_name" = "Horizon Dr"; "short_name" = "Horizon Dr"; types = ( route ); }, { "long_name" = "King of Prussia"; "short_name" = "King of Prussia"; types = ( locality, political ); }, { "long_name" = "Upper Merion"; "short_name" = "Upper Merion"; types = ( "administrative_area_level_3", political ); }, { "long_name" = Montgomery; "short_name" = Montgomery; types = ( "administrative_area_level_2", political ); }, { "long_name" = Pennsylvania; "short_name" = PA; types = ( "administrative_area_level_1", political ); }, { "long_name" = "United States"; "short_name" = US; types = ( country, political ); }, { "long_name" = 19406; "short_name" = 19406; types = ( "postal_code" ); } ); "formatted_address" = "2900 Horizon Dr, King of Prussia, PA 19406, USA"; geometry = { location = { lat = "40.0896985"; lng = "-75.341717"; }; "location_type" = ROOFTOP; viewport = { northeast = { lat = "40.09104748029149"; lng = "-75.34036801970849"; }; southwest = { lat = "40.0883495197085"; lng = "-75.34306598029151"; }; }; }; types = ( "street_address" ); } ); status = OK; }
and the url I am passing:
http://maps.googleapis.com/maps/api/geocode/json?address=2900+Horizon+Drive+King+of+Prussia+,+PA&sensor=false
any idea what I am doing wrong?