首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SLRequest推特iOS

SLRequest推特iOS
EN

Stack Overflow用户
提问于 2013-06-26 00:37:41
回答 2查看 1.2K关注 0票数 1

我正在下载我的iOS应用程序的用户时间表。现在,我正在从Twitter的API URL https://api.twitter.com/1.1/statuses/home_timeline.json获得完整的JSON响应

它将返回所有内容,如下所示...

代码语言:javascript
复制
Timeline response: (
            {
            contributors = "<null>";
            coordinates = "<null>";
            "created_at" = "Sat Jun 08 03:59:36 +0000 2013";
            entities =         {
                hashtags =             (
                );
                symbols =             (
                );
                urls =             (
                                    {
                        "display_url" = "vine.co/v/bLrw1IjLKVl";
                        "expanded_url" = "https://vine.co/v/bLrw1IjLKVl";
                        indices =                     (
                            36,
                            59
                        );
                        url = "https://t.co/8yHzCzMFHC";
                    }
                );
                "user_mentions" =             (
                );
            };
            "favorite_count" = 3;
            favorited = 0;
            geo = "<null>";
            id = 343215709989507073;
            "id_str" = 343215709989507073;
            "in_reply_to_screen_name" = "<null>";
            "in_reply_to_status_id" = "<null>";
            "in_reply_to_status_id_str" = "<null>";
            "in_reply_to_user_id" = "<null>";
            "in_reply_to_user_id_str" = "<null>";
            lang = en;
            place = "<null>";
            "possibly_sensitive" = 0;
            "retweet_count" = 1;
            retweeted = 0;
            source = "<a href=\"http://vine.co\" rel=\"nofollow\">Vine - Make a Scene</a>";
            text = "Black people neighborhoods at night https://t.co/8yHzCzMFHC";
            truncated = 0;
            user =         {
                id = 1129039734;
                "id_str" = 1129039734;
            };
        }
    )

但我只需要"text“参数。我如何才能只获取tweet的文本?

谢谢!

-Henry

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-07-16 01:33:17

这就是我最终使用的解决方案。

代码语言:javascript
复制
NSDictionary *timelineData = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&jsonError];
                            if (timelineData) {
                                tweets = [NSMutableArray new];
                                for (NSDictionary * obj in timelineData) {
                                    NSString *text = [obj objectForKey:@"text"];
                                    [tweets addObject:text];
                                }
                                [self updateTableView];
                            }

希望这能在未来帮助一些人。

票数 0
EN

Stack Overflow用户

发布于 2013-06-26 00:42:49

实现一个JSON Parser,它解析出您需要的内容并丢弃其余的内容,例如yajl。有很多例子。Yajl很快..。

丑陋的解决方案是使用NSString消息: componentsSeparatedByString来拆分包含“NSString =”上的json的文本,然后使用"truncated =“来获得中间的文本...

使用NSJSONSerialization,twitter here有一个很好的例子。

代码语言:javascript
复制
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL
  URLWithString:@"http://api.twitter.com/1/statuses/user_timeline.json?
  screen_name=xx"]];

NSData *response = [NSURLConnection sendSynchronousRequest:request
  returningResponse:nil error:nil];

NSError *jsonParsingError = nil;
NSArray *publicTimeline = [NSJSONSerialization JSONObjectWithData:response
  options:0 error:&jsonParsingError];
NSDictionary *tweet;
for(int i=0; i<[publicTimeline count];i++)
{
    tweet= [publicTimeline objectAtIndex:i];
    NSLog(@”Statuses: %@”, [tweet objectForKey:@"text"]);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17302865

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档