I can retrieve the JSON object and display it, but how do I get the value from "lat" and "lng" to use in Xcode?
My Code:
NSString *str=[NSString stringWithFormat:@"https://www.<WEBSITE>];
NSURL *url=[NSURL URLWithString:str];
NSData *data=[NSData dataWithContentsOfURL:url];
NSError *error=nil;
NSDictionary* dictionary = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:&error];
NSLog(@"Your JSON Object: %@ Or Error is: %@", dictionary, error);
JSON Object:
(
{
response = {
lat = "52.517681";
lng = "-115.113995";
};
}
)
I cant seem to access any of the data. I've tried:
NSLog(@"Value : %@",[dictionary objectForKey:@"response"]);
I've also tried a bunch of variations like
NSLog(@"Value : %@",[[dictionary objectForKey:@"response"] objectForKey:@"lat"]);
But it always ends up with a crash:
-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x15dd8b80
I've noticed when I am going through debugging that 'dictionary' only consists 1 object. How do I convert my JSON object into a NSDictionary with key pairings? Is this JSON object in the wrong format or something?
It seems that the JSON returned is not an NSDictionnary object, but an NSArray. Change your code like this :
The responseArray contains a dictionnary object ( or many ?) then you acn acces theme like this :
That specific JSON object is an NSArray, not an NSDictionary, which is why it does not recognize the selector, and you're not getting a warning because NSJSONSerialization JSONObjectWithData returns an id.
Try
Here is a snip from an app I wrote...basically get an NSData object, typically from a response, and use NSJSONSerialization JSONObjectWithData:Options:Error which will produce an NSDictionary