質問

I am downloading a users timeline for my iOS app. Right now I am getting the full JSON response from Twitter's API URL https://api.twitter.com/1.1/statuses/home_timeline.json

It is returning everything as shown here...

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

But I only want the "text" parameter. How do I only get the text of the tweets?

Thanks!

-Henry

役に立ちましたか?

解決 2

This is the solution I ended up using...

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

Hopefully this helps a few people in the future.

他のヒント

Implement a JSON Parser that parses out what you need and discards the rest, for example yajl. There are plenty of examples. Yajl is fast...

The ugly solution would be to use the NSString message: componentsSeparatedByString to split the NSString containing your json on "text = " and then on "truncated = " to get the text in between...

Using NSJSONSerialization, there is an excellent example for 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"]);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top