我将如何遍历 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]);
}];
}