美文网首页
JSON数据的解析

JSON数据的解析

作者: __DREAM | 来源:发表于2018-04-07 11:14 被阅读0次

// 全局变量

NSDictionary *_dic;

// 创建表格

 self.tableView = [[UITableView alloc]initWithFrame:self.view.frame style:UITableViewStyleGrouped];

// 数据请求

    NSURLSessionDataTask *task = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:(JSON_URL)] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {

// jsonkit解析

        _dic= [data objectFromJSONData];

        dispatch_async(dispatch_get_main_queue(), ^{

            [self.tableViewreloadData];

        });

    }];

    [taskresume];

// 设置表格

-(NSInteger)numberOfSectionsInTableView:(UITableView*)tableView{

    return_dic.count;

}

-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{

    return [[_dic objectForKey:[_dic.allKeys objectAtIndex:section]] count];

}

-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];

    if(!cell) {

        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell"];

    }

    NSArray*arr = [_dicobjectForKey:[_dic.allKeysobjectAtIndex:indexPath.section]];

    cell.textLabel.text= [arr[indexPath.row]objectForKey:@"name"];

    cell.detailTextLabel.text= [arr[indexPath.row]objectForKey:@"like"];

    return cell;

}

-(NSString*)tableView:(UITableView*)tableView titleForHeaderInSection:(NSInteger)section{

    return [_dic allKeys][section];

}

相关文章

网友评论

      本文标题:JSON数据的解析

      本文链接:https://www.haomeiwen.com/subject/tszehftx.html