【问题标题】:SLRequest Twitter iOSSLRequest Twitter iOS
【发布时间】:2013-06-22 13:50:19
【问题描述】:

我正在为我的 iOS 应用下载用户时间线。现在,我正在从 Twitter 的 API URL https://api.twitter.com/1.1/statuses/home_timeline.json

获得完整的 JSON 响应

它正在返回此处显示的所有内容...

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

但我只想要“文本”参数。如何只获取推文的文本?

谢谢!

-亨利

【问题讨论】:

    标签: ios objective-c json twitter slrequest


    【解决方案1】:

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

    丑陋的解决方案是使用 NSString 消息: componentsSeparatedByString 将包含您的 json 的 NSString 拆分为“text =”,然后在“truncated =”上获取文本...

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

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

    【讨论】:

    • 其实 iOS 现在有一个内置的 API JSON 解析器,所以这不是最好的方法。
    • @Henry:你是对的,JSON 支持现在是 iOS 原生的 NSJSONSerialization。
    • 那么我将如何解析所有这些“推文”以获得文本值?
    【解决方案2】:

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

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

    希望这对未来的一些人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-10
      • 1970-01-01
      • 2014-05-03
      • 2013-06-01
      • 2013-11-30
      • 2012-12-25
      • 1970-01-01
      相关资源
      最近更新 更多