2

我正在尝试从此URL解析 JSON ,这是 Wordpress 的 JSON 输出。由于某种原因,UITableView 中没有输出。

这是我必须解析的代码。我确定我错过了一些东西,但我无法弄清楚如何在这段代码中解析嵌套的 JSON。

视图控制器.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;
}

视图控制器.h

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController {
    IBOutlet UITableView *mainTableView;

    NSArray *news;
    NSMutableData *data;
}

@end

请帮我解决这个问题。

非常感谢!我试过在 Stackoverflow 上引用多个线程,但都没有为我工作。

PS:我是iOS应用开发的新手。我正在使用这个视频教程来弄乱代码。

4

1 回答 1

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表示 a 的响应中看到 { 时Dictionary,a [ 表示Array. TableViewsArray通常填充。所以你可以看到你有一个带有几个键值对的JSON响应,其中一个是of的DictionaryArrayDictionary

您需要为新闻对象分配该 Dictionary 键的内容:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    NSDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil];
    news = [responseDict objectForKey@"posts"];
    [mainTableView reloadData];
}
于 2012-12-21T17:11:57.083 回答