json解析

#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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值