SLRequest Twitter iOS

时间:2013-06-25 16:37:41

标签: ios objective-c json twitter slrequest

我正在为我的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;
            };
        }
    )

但我只想要“text”参数。我如何才能获得推文的文字?

谢谢!

-Henry

2 个答案:

答案 0 :(得分:0)

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

丑陋的解决方案是使用NSString消息:componentsSeparatedByString在“text =”上拆分包含json的NSString,然后在“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"]);
}

答案 1 :(得分:0)

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

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

希望将来可以帮助一些人。