0

I make this facebook request:

SLRequest *facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook
                                                    requestMethod:SLRequestMethodGET
                                                              URL:[NSURL URLWithString:@"https://graph.facebook.com/fql"]
                                                       parameters:[NSDictionary dictionaryWithObject:@"SELECT uid, name, pic_square FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = me())" forKey:@"q"]];

[facebookRequest setAccount:_facebookAccount];

[facebookRequest performRequestWithHandler:^(NSData* responseData, NSHTTPURLResponse* urlResponse, NSError* error) {
     if(!error){
           id json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
           NSLog(@"%@", [json class]); 
           //NSCFDictionary. How can i get the data that's inside this?
     }else{
           NSLog(@"error fb");
            }
     }];

Any thoughts on how to retrieve the data inside the NSCFDictionary? I've tried [json valueforKey...] but nothing

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Pedro Vieira
  • 3,330
  • 3
  • 41
  • 76

1 Answers1

3

Use NSDictionary's -objectForKey: method:

id json = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil];
if ([json isKindOfClass:[NSDictionary class]]) {
    NSDictionary *dict = (NSDictionary*)json;
    for (NSString *key in dict) {
        id object = [dict objectForKey:key];
        NSLog(@"The object for key \"%@\" is: %@", key, object);
    }
}

Incidentally, it wouldn't take much to figure out that a NSCFDictionary can be treated like a NSDictionary. Here's one SO question that explains that: What is an NSCFDictionary? Once you know that, you can easily figure out how to deal with it by looking at the documentation for NSDictionary.

Community
  • 1
  • 1
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • 1
    thanks man! it does it's job. the weird thing is that `NSLog` logs only 1 time and it logs everything. i use this code to get the first 'uid', 'pic_square' and 'name' set of data `NSDictionary *dict = (NSDictionary*)[NSJSONSerialization JSONObjectWithData:responseData options:0 error:nil]; for (NSString *key in dict) { id object = [dict objectForKey:key]; NSLog(@"The object for key is: %@", [object objectAtIndex:0]); }` – Pedro Vieira Nov 09 '12 at 01:24