Lord Vermillion
Lord Vermillion

Reputation: 5414

Getting json data from NSDictionary

I'm trying to get data from a simple json response.

The first log shows that i find an item named access_token, however i can't use objectForKey from it, i get [__NSCFString objectForKey:]: unrecognized selector sent to instance 0x72de3b0

What am i doing wrong?

 for(NSDictionary *item in authResponse)
    {
        NSString *s1 = @"access_token";
        NSString *s2 = [item description];

        NSLog(@"access desc: %@", [item description]);
        if([s1 isEqualToString:s2]){
            NSLog(@"Item: %@", [item objectForKey:@"access_token"]);
        }
    }

Upvotes: 0

Views: 89

Answers (1)

trojanfoe
trojanfoe

Reputation: 122381

The authResponse object is an NSDictionary object, however it will contain many other types within it, including other NSDictionary objects. You will need to be more careful about how you interpret each member:

for (NSObject *item in authResponse)
{
    if ([item isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *dictItem = (NSDictionary *)item;
        //  do thing with sub-dictionary
    }
    else if ([item isKindOfClass:[NSString class]])
    {
        NSString *stringItem = (NSString *)item;
        //  do thing with string item
    }
    else if ([item isKindOfClass:[NSNumber class]])
    {
        NSNumber *numberItem = (NSNumber *)item;
        //  do thing with number item
    }
}

Upvotes: 1

Related Questions