• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

objective-c - 在 iOS 上将 Wordpress JSON 解析为 UITableView

[复制链接]
菜鸟教程小白 发表于 2022-12-13 00:04:44 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试解析来自 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)connectionNSURLConnection *)connection didReceiveResponseNSURLResponse *)response
{
    data = [[NSMutableData alloc] init];
}

- (void)connectionNSURLConnection *)connection didReceiveDataNSData *)theData
{
    [data appendData:theData];
}

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

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

- (void)connectionNSURLConnection *)connection didFailWithErrorNSError *)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)numberOfSectionsInTableViewUITableView *)tableView
{
    return 1;
}

- (int)tableViewUITableView *)tableView numberOfRowsInSectionNSInteger)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乱码。



Best Answer-推荐答案


我明白问题所在了。新闻对象是一个 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 填充。所以你可以看到你有一个 DictionaryJSON 响应,其中有几个键值对,其中一个是 Array >字典

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

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

关于objective-c - 在 iOS 上将 Wordpress JSON 解析为 UITableView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13994032/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap