解析JSON消息以检索iOS 5上的Twitter数据
我正在尝试在我的应用程序中显示从中检索到的一些公共推文。为了检索twitter数据,我实现了以下解析JSON消息以检索iOS 5上的Twitter数据,ios,json,parsing,twitter,ios5,Ios,Json,Parsing,Twitter,Ios5,我正在尝试在我的应用程序中显示从中检索到的一些公共推文。为了检索twitter数据,我实现了以下getTweets方法: -(void)getTweets { NSMutableDictionary *params = [[NSMutableDictionary alloc] init]; [params setObject:@"Obamabarak" forKey:@"screen_name"]; [params setObject:@"10" forKey:@"cou
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"]);
}
我在网上找不到任何好的例子,但这对我很有用
雅萨试试这个
#define jsonQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
#define jsonURL [NSURL URLWithString: @"Your LInk"]
@synthesize YOUR NSDICTIONARY;
- (void)issueLoadRequest
{
// Dispatch this block asynchronosly. The block gets JSON data from the specified URL and performs the proper selector when done.
dispatch_async(jsonQueue, ^{
NSData* data = [NSData dataWithContentsOfURL: jsonURL];
[self performSelectorOnMainThread:@selector(receiveData:) withObject:data waitUntilDone:YES];
});
}
- (void)receiveData:(NSData *)data {
// When we have the data, we serialize it into native cocoa objects. (The outermost element from twitter is
// going to be an array. I JUST KNOW THIS. Reload the tableview once we have the data.
self.YOUR NSDICTIONARY= [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
[self.myTableView reloadData];
}
你好,对这个问题有什么建议吗?我还是被困在这上面!:(