【问题标题】:Parse Wordpress JSON into UITableView on iOS在 iOS 上将 Wordpress JSON 解析为 UITableView
【发布时间】:2012-12-21 16:48:15
【问题描述】:

我正在尝试解析来自 this URL 的 JSON,这是来自 Wordpress 的 JSON 输出。由于某种原因,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 应用开发的新手。我正在使用this Video tutorial 来弄乱代码。

【问题讨论】:

  • 如果您遇到任何错误怎么办?在不知道您的头文件是什么样子的情况下,很难仅复制/粘贴您的代码并查看发生了什么。
  • 这样做:NSLog(@"news = %@", [news description]);在您将 news 分配给 JSON 值并发布结果之后(如果结果很大,请发布前几个对象)
  • 我已经用头文件更新了问题。还有@ElJay,我该怎么做?你能帮忙吗?

标签: objective-c ios json wordpress


【解决方案1】:

我明白问题所在了。新闻对象是一个Dictionary,它有几个键值对,其中一个是帖子,它实际上包含您要用来填充TableViewArray

这是您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 填充。所以你可以看到你有一个DictionaryJSON 响应,其中有几个键值对,其中一个是ArrayDictionary

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

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

【讨论】:

  • 太棒了。它就像我想要的那样工作!非常感谢!非常感谢。
  • 任何时候,这是一个很好的问题,您发布的代码(以及该链接)帮助很快解决了问题:)
  • 再次感谢。现在想办法显示帖子中的图像。
  • 看看我对这个 SO 问题的回答:stackoverflow.com/questions/9786018/…我为此写了一个类。
  • 另一个快速查询:如何访问 JSON 中的内部数组。喜欢缩略图和作者姓名?你能帮我解决这个问题吗? @ElJay
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
  • 2020-03-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多