#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *table;
@property (weak, nonatomic) IBOutlet UITextView *textV;
@property(nonatomic,strong)NSArray *dataSouce;
@end
@implementation ViewController
//点击按钮 解析
- (IBAction)jiexi:(id)sender {
//创建路径
NSURL *url = [NSURL URLWithString:@"http://127.0.0.1/book.json"];
//读取文件
NSData *data = [NSData dataWithContentsOfURL:url];
//初始化数据
self.dataSouce = [NSArray array];
NSError *error = nil;
//解析json数据 将json转换为oc对象 序列化
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error];
NSLog(@"dict %@",dict);
NSArray *arr = dict[@"applications"];
NSLog(@"arr %@",arr);
self.dataSouce = arr;
}
//生成 反序列化
- (IBAction)shengcheng:(id)sender {
//--->定义一个oc对象@[]
// NSArray *b = @[@{@"fds":@"vfds"},@{@"abc":@"cd"}];
NSArray * books = [NSArray arrayWithObjects:
[NSDictionary dictionaryWithObjectsAndKeys:@"旋风小子" , @"title",
@"刘浩" , @"author" , @"这本是7天写出6个字", @"remark" , nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"卖火柴的小女孩", @"title",
@"邱秋生" , @"author" ,
@"本书来自作者邱秋生,根据本人的亲身经历来写的",
@"remark" , nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"脸皮是怎么炼成的",
@"title" , @"陈鑫", @"author",
@"本书主要介绍陈鑫结实的脸皮",
@"remark" , nil],nil];
//反序列化处理
NSData *data = [NSJSONSerialization dataWithJSONObject:books options:0 error:nil];
// 将二进制数据转换为字符串并且打印
NSLog(@"string : %@",[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]);
}
- (void)viewDidLoad {
[super viewDidLoad];
//设置代理
self.table.delegate = self;
self.table.dataSource = self;
}
#pragma mark - 数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 10;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
}
NSDictionary *dic = self.dataSouce[0];
cell.textLabel.text = dic[@"categoryName"];
return cell;
}
@end