How to get a value from a JSON Object using NSJSON

2020-07-17 05:16发布

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?

3条回答
一纸荒年 Trace。
2楼-- · 2020-07-17 05:23

It seems that the JSON returned is not an NSDictionnary object, but an NSArray. Change your code like this :

NSarray *responseArray = [NSJSONSerialization JSONObjectWithData:data
                                                           options:kNilOptions
                                                             error:&error];

The responseArray contains a dictionnary object ( or many ?) then you acn acces theme like this :

For ( NSDictionary *dic in responseArray){
    NSDictionnary *response = [dic objectForKey@"response"];
....
}
查看更多
啃猪蹄的小仙女
3楼-- · 2020-07-17 05:30

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

NSArray *array = [NSJSONSerialization JSONObjectWithData:data
                                                 options:kNilOptions
                                                   error:&error];
NSDictionary *dictionary = array[0];
查看更多
乱世女痞
4楼-- · 2020-07-17 05:42

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

NSError *error = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];

//Make sure response came in
if(response != nil){
    NSError *error = nil;
    id data = [NSJSONSerialization JSONObjectWithData:response options:0 error:&error];

    if([data isKindOfClass:[NSDictionary class]]){
        //Create dictionary from all the wonderful things google provides
        NSDictionary *results = data;

        //Search JSON here
        NSArray *cityAndState = [[[[results objectForKey:@"results"] objectAtIndex:0] objectForKey:@"address_components"] valueForKey:@"short_name"];
查看更多
登录 后发表回答