json解析

这里写图片描述

#import "ViewController.h"
#import "JSONKit.h"
#import "SBJsonParser.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>{
    UITableView *_table;
    NSMutableDictionary *_dic;
}

@end
#define JSON_URL @"http://127.0.0.1/1509E.json"
//#define JSON_URL @"http://www.zhanqi.tv/api/static/live.index/recommend-apps.json?"
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 初始化表格
    _table = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];

    _table.delegate = self;
    _table.dataSource = self;

    [self.view addSubview:_table];

    _dic = [[NSMutableDictionary alloc]init];

    NSURL *url = [NSURL URLWithString:JSON_URL];

    NSURLRequest *request = [[NSURLRequest alloc]initWithURL:url];

    [NSURLConnection sendAsynchronousRequest:request queue:[[NSOperationQueue alloc]init] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {

        NSLog(@"异步加载数据----->%@",response.MIMEType);

        if ([response.MIMEType isEqualToString:@"application/json"]) {

#if 0
        // 初始化一个 SBJsonParser 的对象
        SBJsonParser *parser = [[SBJsonParser alloc]init];
        _dic = [parser objectWithData:data];
            NSLog(@"解析数据---->>>%@",_dic);
#elif 0
            // JSONKit
            _dic = [data objectFromJSONData];

#elif 1
            // 系统自带的 Json 解析
            _dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

#endif
            // 回到主线程刷新表格
            dispatch_async(dispatch_get_main_queue(), ^{
                [_table reloadData];
            });
        }
    }];

}
// 设置表格分区
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dic.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSString *key = [_dic.allKeys objectAtIndex:section];
    return [[_dic objectForKey:key]count];
}
// 设置表格单元格
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuseID = @"";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:reuseID];
    }

    NSArray *arr = [_dic objectForKey:[_dic.allKeys objectAtIndex:indexPath.section]];
//    cell.textLabel.text = [[arr objectAtIndex:indexPath.row]objectForKey:@"name"];
//    cell.detailTextLabel.text = [[arr objectAtIndex:indexPath.row]objectForKey:@"ids"];
        //cell.textLabel.text = [_dic.allKeys objectAtIndex:indexPath.section];
    NSLog(@"<->-<>-->%@",[[[_dic objectForKey:_dic.allKeys [indexPath.section]]objectAtIndex:indexPath.row] objectForKey:@"ids"]) ;
    cell.textLabel.text = _dic.allKeys[indexPath.section];
        cell.detailTextLabel.text = [[arr objectAtIndex:indexPath.row]objectForKey:@"name"];
    return cell;
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值