簡體   English   中英

SL請求Twitter iOS

[英]SLRequest Twitter iOS

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

但是我只想要“文本”參數。 我如何只獲取推文的文字?

謝謝!

-亨利

實現一個JSON分析器,該分析器解析出您需要的內容並丟棄其余內容,例如yajl 有很多例子。 Yajl很快...

丑陋的解決方案是使用NSString消息:componentsSeparatedByString將包含您的json的NSString拆分為“ text =”,然后拆分為“ truncated =”,以在兩者之間獲取文本。

使用NSJSONSerialization,對於Twitter的一個很好的例子在這里

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

這是我最終使用的解決方案...

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

希望這對將來的人有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM