在Cocoa中集成Twitter搜索API

时间:2013-05-28 08:03:38

标签: ios cocoa twitter hashtag

我希望获得带有特定主题标签的推文。我使用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;       page = 1;       query =“%23zesdaagsegent”;       “refresh_url”=“?since_id = 339031661368975360&amp; 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;               metadata = {                   “result_type”=最近;               };               source =“&lt; a href =”http://twitter.com/">web</a&gt;“;               text =“#zesdaagsegent Het is meen zonnige dag hier in Merelbeke。我们发表了关于他们的信息naar een beetje zonnestralen in het   gezicht,toch?“;           }       );       “results_per_page”= 15;       “since_id”= 0;       “since_id_str”= 0; }

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

有没有人有想法?

1 个答案:

答案 0 :(得分:2)

试试这个:

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

你应该有一个NSArray results包含每个匹配推文的X NSDictionary。

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