我想获取带有特定主题标签的推文。我使用Twitter搜索URL。这是我的代码:

  NSMutableString *urlString = [NSMutableString stringWithFormat:@"%s","http://search.twitter.com/search.json?q=%23zesdaagsegent"];
    NSURL *url = [NSURL URLWithString:urlString];
    NSData *data = [NSData dataWithContentsOfURL:url];
    NSLog(@"%@", data);
    NSError *error;
    NSMutableArray *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
    NSLog(@"%@", json);

    NSMutableArray *results =[NSMutableArray array];
    for(NSDictionary *item in json)
    {
        [results addObject:[item objectForKey:@"results"]];
    }

我的NSLog为我提供了我需要的输出:

2013-05-28 09:48:45.080 ZesdaagseGent [572:11303] {
“completed_in” =“0.023”;
“max_id” = 339031661368975360;
“max_id_str” = 339031661368975360;
页面= 1;
query =“%23zesdaagsegent”;
“refresh_url” =“?since_id = 339031661368975360&q =%23zesdaagsegent”;
结果=(
{
“created_at” =“2013年5月27日星期一14:53:41 +0000”;
“from_user” = SigfridMaenhout;
“from_user_id” = 369194526;
“from_user_id_str” = 369194526;
“from_user_name” =“Sigfrid Maenhout”;
geo =“”;
id = 339031661368975360;
“id_str” = 339031661368975360;
“iso_language_code” = nl;
元数据= {
“result_type” =最近;
};
source =“http://twitter.com/">web</a&gt;“;
text =“#zesdaagsegent Het是Merelbeke中的zonnige dag hier。我们在het中用语言表达了异体变种naar een beetje zonnestralen
盖济希特,火炬手?”;
}
);
“results_per_page” = 15;
“since_id” = 0;
“since_id_str” = 0; }

我需要带有对象的数组“结果”。我的问题是,我无法使用objectForKey方法在NSMutableArray中获得结果。

有人有主意吗?

最佳答案

试试这个 :

NSMutableArray *results = [NSMutableArray array];
NSDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:data
                                                             options:kNilOptions
                                                               error:&error];
for (NSDictionary *result in jsonResponse[@"results"]) {
    [results addObject:result];
}

而且,您应该为每个匹配的tweet使用一个包含X NSDictionary的NSArray results

PS:[@“results”]是[NSDictionary objectForKey:@“results”]的现代Objective-C语法

关于ios - 在 cocoa 中集成Twitter搜索API,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16786886/

10-13 04:08