概述
JSON数据,手动解析并展示在列表中
示例
先看图
工程目录结构
示例代码
User.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
//用户数据模型
@interface User : NSObject{
//声明成员变量
//名称
NSString* _name;
//年龄
NSString* _age;
//id
NSString* _useId;
}
//属性对象
@property(retain,nonatomic) NSString* name;
@property(retain,nonatomic) NSString* age;
@property(retain,nonatomic) NSString* userID;
@end
NS_ASSUME_NONNULL_END
User.m
#import "User.h"
@implementation User
//同步属性和成员变量
@synthesize name = _name;
@synthesize age = _age;
@synthesize userID = _useId;
@end
CategoryBean.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface CategoryBean : NSObject
{
NSString* _title;
NSMutableArray* _arrayUser;
}
@property NSString* title;
@property NSMutableArray* arrayUser;
@end
NS_ASSUME_NONNULL_END
CategoryBean.m
#import "CategoryBean.h"
@implementation CategoryBean
@synthesize title = _title;
@synthesize arrayUser = _arrayUser;
@end
ViewController.h
#import <UIKit/UIKit.h>
#import "CategoryBean.h"
//实现两个代理
@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource>
{
//列表view
UITableView* _tabV;
//类别对象
CategoryBean* _categoryBean;
}
@end
ViewController.m
#import "ViewController.h"
#import "User.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//创建列表控件
_tabV = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
_tabV.delegate = self;
_tabV.dataSource = self;
[self.view addSubview:_tabV];
//解析JSON数据
[self parseData];
[_tabV reloadData];
}
//数组数量
-(NSInteger) numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
//每个数组中元素个数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return _categoryBean.arrayUser.count;
}
//添加一个头部view
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
return _categoryBean.title;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString* strID = @"ID";
UITableViewCell* cell = [_tabV dequeueReusableCellWithIdentifier:strID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:strID];
}
//获取对应的用户
User* user = [_categoryBean.arrayUser objectAtIndex:indexPath.row];
//显示title为用户名
cell.textLabel.text = user.name;
cell.detailTextLabel.text = user.age;
return cell;
}
//解析JSON数据函数
-(void) parseData{
//获取json文件本地路径
NSString* path = [[NSBundle mainBundle] pathForResource:@"gong" ofType:@"json"];
//将本地json文件加载为二进制文件
NSData* data = [NSData dataWithContentsOfFile:path];
//解析为字典格式
NSDictionary* dicRoot = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//下面是手动解析JSON,这个过程和Java,dart中的思想是一样的,只是各自语言中api不用。
//OC中的字典就相当于Java中JsonObject。
//判断是否为字典
if ([dicRoot isKindOfClass:[NSDictionary class]]) {
//开始解析
NSArray* categories = [dicRoot objectForKey:@"categories"];
for (int i=0; i<categories.count; i++) {
NSDictionary* dicCategory = [categories objectAtIndex:i];
_categoryBean = [CategoryBean new];
NSString* title = [dicCategory objectForKey:@"title"];
[_categoryBean setTitle:title];
NSArray* details = [dicCategory objectForKey:@"detail"];
NSMutableArray* arrayUsers = [NSMutableArray new];
for (int j=0; j<details.count; j++) {
NSDictionary* dicUser = [details objectAtIndex:j];
User* user = [User new];
user.userID = [dicUser objectForKey:@"id"];
user.name = [dicUser objectForKey:@"name"];
user.age = [dicUser objectForKey:@"age"];
[arrayUsers addObject:user];
}
_categoryBean.arrayUser = arrayUsers;
}
}
}
@end
Json数据
{
"categories": [
{
"id": "",
"detail": [
{
"id": "27230907",
"name": "张三",
"age": "30",
},
{
"id": "27230907",
"name": "李四",
"age": "32",
},
{
"id": "27230907",
"name": "赵五",
"age": "40",
},
{
"id": "27230907",
"name": "王六",
"age": "30",
},{
"id": "27230907",
"name": "李世明",
"age": "39",
},
{
"id": "27230907",
"name": "孔子",
"age": "77",
},
{
"id": "27230907",
"name": "老子",
"age": "90",
},
{
"id": "27230907",
"name": "李志",
"age": "50",
},
],
"title": "用户列表",
"imgs": "",
}
],
"totalcount": "10000",
"page": "1",
"status": 200,
"message": "success"
}