0

我提出这个 Facebook 请求:

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");
            }
     }];

关于如何检索 NSCFDictionary 中的数据的任何想法?我试过[json valueforKey...]但没有

4

1 回答 1

3

使用 NSDictionary 的-objectForKey:方法:

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);
    }
}

顺便说一句,不费吹灰之力就能发现 NSCFDictionary 可以像 NSDictionary 一样对待。这里有一个 SO 问题可以解释:什么是 NSCFDictionary?一旦你知道了这一点,你就可以通过查看 NSDictionary 的文档轻松找出如何处理它。

于 2012-11-09T01:14:34.143 回答