我正在尝试在我的应用程序中显示从 Obama twitter profile 检索到的一些公共(public)推文.为了检索推特数据,我实现了这个 getTweets
方法:
-(void)getTweets
{
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[params setObject:@"Obamabarak" forKey:@"screen_name"];
[params setObject:@"10" forKey:@"count"];
[params setObject:@"1" forKey:@"include_entities"];
[params setObject:@"1" forKey:@"include_rts"];
NSURL *url = [NSURL URLWithString:@"http://api.twitter.com/1/statuses/user_timeline.json"];
TWRequest *request = [[TWRequest alloc] initWithURL:url parameters:params requestMethod:TWRequestMethodGET];
[request performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error)
{
if (error != nil)
{
// Inspect the contents of error
exit(-1);
}
else
{
[self fetchJSONData:responseData];
}
}
}
此时,我尝试按如下方式实现 fetchJSONData
方法:
- (void)fetchJSONData:(NSData *)responseData
{
NSError* error;
NSDictionary* jsonResults = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray *myArrayOfDictionaries = [[jsonResults objectForKey:@"tweets"] objectForKey:@"results"];
for (NSDictionary *myDictionary in myArrayOfDictionaries)
{
// Get title of the image
NSString *title = [myDictionary objectForKey:@"title"];
...
但是不起作用,没有显示任何记录。我不确定这是否是正确的方法,我不知道如何进行。你能帮我找到一种显示奥巴马推文的方法吗?
提前致谢
最佳答案
我终于摆脱了这个问题:
if ([urlResponse statusCode] == 200)
{
// Parse the responseData, which we asked to be in JSON format for this request
NSError *jsonParsingError = nil;
//this is an array of dictionaries
arrayTweets = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
//point to the first tweet
NSDictionary *aTweet = [arrayTweets objectAtIndex:0];
//write to log
NSLog(@"text: %@", [aTweet objectForKey:@"text"]);
NSLog(@"created_at: %@", [aTweet objectForKey:@"created_at"]);
}
我没有在互联网上找到任何好的示例,但这对我有用!!!
亚萨
关于ios - 解析 JSON 消息以在 iOS 5 上检索 Twitter 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8656286/