1

我将如何遍历 TWRequest 返回的 JSON 以获取推文的地理信息?我正在使用下面的代码 - 我已经标记了我不确定的位。文本组件工作正常,我只是不确定如何创建地理数据数组并访问它......

    - (void)fetchTweets
{
    AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];


    //NSLog(@"phrase carried over is %@", delegate.a);

    // Do a simple search, using the Twitter API
    TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:
                                                         [NSString stringWithFormat:@"http://search.twitter.com/search.json?q=%@", delegate.a]] 
                                             parameters:nil requestMethod:TWRequestMethodGET];

    // Notice this is a block, it is the handler to process the response
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
     {
         if ([urlResponse statusCode] == 200) 
         {
             // The response from Twitter is in JSON format
             // Move the response into a dictionary and print
             NSError *error;        
             NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
             //NSLog(@"Twitter response: %@", dict);

             NSArray *results = [dict objectForKey:@"results"];


             //Loop through the results


             for (NSDictionary *tweet in results) {
                 // Get the tweet
                 NSString *twittext = [tweet objectForKey:@"text"];
                 //added this one - need to check id NSString is ok??
                 NSString *twitlocation = [tweet objectForKey:@"geo"];

                 // Save the tweet to the twitterText array
                 [_twitterText addObject:twittext];

                 //this is the loop for the location
                [twitterLocation addObject:twitlocation];
             }

             dispatch_async(dispatch_get_main_queue(), ^{

[self.tableView reloadData];

             });




         }

         else
             NSLog(@"Twitter error, HTTP response: %i", [urlResponse statusCode]);
     }];



}
4

1 回答 1

1

“geo”已被弃用,可能根本没有填充。我记得它在 Twitter API v1.0 中也被弃用了。试试这个代码:

- (void)fetchTweets
{
    AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication]delegate];

    //NSLog(@"phrase carried over is %@", delegate.a);

    // Do a simple search, using the Twitter API
    TWRequest *request = [[TWRequest alloc] initWithURL:[NSURL URLWithString:
                                                         [NSString stringWithFormat:@"http://search.twitter.com/search.json?q=%@", delegate.a]]
                                             parameters:nil requestMethod:TWRequestMethodGET];

    // Notice this is a block, it is the handler to process the response
    [request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
     {
         if ([urlResponse statusCode] == 200)
         {
             // The response from Twitter is in JSON format
             // Move the response into a dictionary and print
             NSError *error;
             NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&error];
             //NSLog(@"Twitter response: %@", dict);

             NSArray *results = [dict objectForKey:@"results"];


             //Loop through the results


             for (NSDictionary *tweet in results) {
                 // Get the tweet
                 NSString *twittext = [tweet objectForKey:@"text"];
                 //added this one - need to check id NSString is ok??
                 id jsonResult = [tweet valueForKeyPath:@"coordinates.coordinates"];
                 if ([NSNull null] != jsonResult) {
                     if (2 == [jsonResult count]) {
                         NSDecimalNumber* longitude = [jsonResult objectAtIndex:0];
                         NSDecimalNumber* latitude = [jsonResult objectAtIndex:1];
                         if (longitude && latitude) {
                             // here you have your coordinates do whatever you like
                             [twitterLocation addObject:[NSString stringWithFormat:@"%@,%@", latitude, longitude]];
                         }
                         else {
                             NSLog(@"Warning: bad coordinates: %@", jsonResult);
                         }
                     }
                     else {
                         NSLog(@"Warning: bad coordinates: %@", jsonResult);
                     }
                 }

                 // Save the tweet to the twitterText array
                 [_twitterText addObject:twittext];

             }

             dispatch_async(dispatch_get_main_queue(), ^{
                 [self.tableView reloadData];
             });
         }
         else
             NSLog(@"Twitter error, HTTP response: %i", [urlResponse statusCode]);
     }];
}
于 2012-12-14T19:53:57.900 回答