2012-12-21 39 views
2

我試圖解析來自this URL的JSON,它是Wordpress的JSON輸出。出於某種原因,UITableView中沒有輸出。將Wordpress JSON解析爲iOS上的UITableView

這是我必須解析的代碼。我確定我錯過了一些東西,但我無法弄清楚如何解析此代碼中的嵌套JSON。

ViewController.m:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    self.title = @"News"; 

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 

    NSURL *url = [NSURL URLWithString:@"http://www.karthikk.net/?json=1"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    data = [[NSMutableData alloc] init]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData 
{ 
    [data appendData:theData]; 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

    news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil]; 
    [mainTableView reloadData]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The download could not complete - please make sure you're connected to either 3G or Wi-Fi." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 
    [errorView show]; 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
} 

- (int)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    return 1; 
} 

- (int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    return [news count]; 
} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"]; 

    if(cell == nil){ 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"]; 
    } 

    cell.textLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"title"]; 
    cell.detailTextLabel.text = [[news objectAtIndex:indexPath.row] objectForKey:@"date"]; 

    return cell; 
} 

ViewController.h

#import <UIKit/UIKit.h> 

@interface ViewController : UIViewController { 
    IBOutlet UITableView *mainTableView; 

    NSArray *news; 
    NSMutableData *data; 
} 

@end 

請幫我在削這一點。

非常感謝!我試過指向Stackoverflow上的多個線程,但都未能爲我工作。

P.S .:我是iOS App開發的新手。我正在使用this Video tutorial來混淆代碼。

+0

如果出現任何錯誤,該怎麼辦?不知道你的頭文件是什麼樣的,只是複製/粘貼你的代碼並看看發生了什麼是很困難的。 – propstm

+0

這樣做:NSLog(@「news =%@」,[news description]);在將新聞分配給JSON值之後並將結果發佈(如果結果很大,請發佈前幾個對象) –

+0

我已經用頭文件更新了問題。和@ElJay,我該怎麼做?能否請你幫忙? –

回答

4

我看到了什麼問題。新聞對象是一個Dictionary,它有幾個鍵值對,其中一個是實際包含Array的帖子,您想用它來填充TableView

這裏是你的JSON響應第一點點:

{"status":"ok","count":10,"count_total":662,"pages":67,"posts":[{"id":6176,"type":"post","slug":"how-to-save-any-photo-from-facebook-to-your-iphone-or-ipad-or-ipod-touch","url"... 

當你看到一個{在JSON響應,指示Dictionary,一【表示ArrayTableViews通常從Array填充。所以,你可以看到你有一個Dictionary幾個鍵值對那些之一的JSON響應Dictionary的的Array

您需要分配的消息對象,該對象字典項的內容:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection { 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
    NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil]; 
    news = [responseDict [email protected]"posts"]; 
    [mainTableView reloadData]; 
} 
+0

輝煌。它的工作就像我想要的那樣工作!非常感謝!非常感謝。 –

+1

隨時,這是一個很好的問題,您發佈的代碼(以及該鏈接)幫助很快找出問題:) –

+0

再次感謝。現在想辦法顯示帖子中的圖片。 –