Reputation: 6384
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?
Upvotes: 4
Views: 1276
Reputation: 31
The difference from the data that you get from google is that it comes separate by keys, such as results, geometry, formatted_address
...
You should do something like this:
NSError *error;
NSString *lookUpString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&components=country:AU&sensor=false", self.searchBar.text];
lookUpString = [lookUpString stringByReplacingOccurrencesOfString:@" " withString:@"+"];
NSData *jsonResponse = [NSData dataWithContentsOfURL:[NSURL URLWithString:lookUpString]];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonResponse options:kNilOptions error:&error];
self.locationArray = [[jsonDict valueForKey:@"results"] valueForKey:@"formatted_address"];
int total = self.locationArray.count;
NSLog(@"locationArray count: %d", self.locationArray.count);
for (int i = 0; i < total; i++)
{
NSString *statusString = [jsonDict valueForKey:@"status"];
NSLog(@"JSON Response Status:%@", statusString);
NSLog(@"Address: %@", [self.locationArray objectAtIndex:i]);
}
Upvotes: 3