如何获取由OSX上的SLRequest返回的NSCFDictionary内的数据? [英] How to get data that's inside NSCFDictionary returned by SLRequest on OSX?

查看:92
本文介绍了如何获取由OSX上的SLRequest返回的NSCFDictionary内的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  SLRequest * facebookRequest = [SLRequest requestForServiceType:SLServiceTypeFacebook 
requestMethod:SLRequestMethodGET
URL:[NSURL URLWithString:@https://graph.facebook.com/fql]
参数:[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。我该如何获取这个数据?
} else {
NSLog(@error fb);
}
}];任何关于如何检索NSCFDictionary内的数据的想法?我试过 [json valueforKey ...] 但没有

解决方案

使用NSDictionary的 -objectForKey:方法:

  id json = [NSJSONSerialization JSONObjectWithData:responseData选项:0 error:nil]; 
if([json isKindOfClass:[NSDictionary class]]){
NSDictionary * dict =(NSDictionary *)json;
for(NSString * key in dict){
id object = [dict objectForKey:key];
NSLog(@键\%@ \的对象是:%@,key,object);
}
}

顺便提一句,一个NSCFDictionary可以像NSDictionary一样对待。这是一个SO问题,解释为:什么是NSCFDictionary?一旦你知道,你可以很容易地弄清楚如何处理通过查看NSDictionary的文档。


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

解决方案

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.

这篇关于如何获取由OSX上的SLRequest返回的NSCFDictionary内的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆