2011-12-28 44 views
0

我試圖在我的應用程序中顯示從Obama twitter profile檢索到的一些公共推文。要獲取Twitter的數據,我實現了這個getTweets方法:解析JSON消息以檢索iOS上的Twitter數據5

-(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"]; 
    ... 

但它不工作,沒有記錄顯示。我不確定這是否正確,我不知道如何繼續。你能幫我找到一種方法來展示奧巴馬的推文嗎?

在此先感謝

+0

您好,任何在這個問題上的建議嗎?我仍然堅持這一點! :( – yassassin 2012-01-02 10:12:49

回答

1

我終於擺脫了這個問題:

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

我沒有在互聯網上找到任何很好的例子,但是這個工作對我來說!

yassa

1

試試這個

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