2012-12-21 36 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

再次感谢。现在想办法显示帖子中的图片。 –