JSon解析 + 数据模型

JSon解析的步骤

    //1.根据网络或本地的URL,获取到二进制数据流
    //2.通过json工具查看数据,并获取最外层数据
    //3. 层层解析得到想要的数据;

//

 NSURL *url = [NSURL URLWithString:LOGIN_URL];
 NSString *path = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];
    //NSLog(@"path = %@",path);

//字符串转NSData: dataUsingEncoding

NSData *data = [path dataUsingEncoding:NSUTF8StringEncoding];

//类似plist,json数据最外层是{},则用字典类型接收;[],则用数组接受
//NSJSONSerialization   json序列化类
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"dic = %@",dic);

//获取某个key对应的值

    NSLog(@"expiretime = %@",dic[@"expiretime"]);
    NSLog(@"message=%@",dic[@"message"]);

JSon 解析test

//1. 获取本地数据流

NSData *data = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:LOCAL_P]];

//2.通过json工具,获取json数据的所有内容

    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

//3. 层层解析

    NSLog(@"name = %@",dic[@"tags"][6][@"name"]);
    NSLog(@"average = %@",dic[@"rating"][@"average"]);
    NSLog(@"summary = %@",dic[@"summary"]);

数据模型实例

//1. 数据数据流

    NSData *data  = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:LOCAL_P]];

//2.查看json,再作json解析

    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

    TagsModel *tags = [[TagsModel alloc] init];

//3.层层解析验证数据,然后存储的数据模型中

    for (NSDictionary *dic2 in dic[@"tags"]) {

       BookModel *book = [[BookModel alloc] init];

       book.num  = dic2[@"count"];
        book.name = dic2[@"name"];
        book.title = dic2[@"title"];

        //将对象存到数组中
        [tags.mutArr addObject:book];
    }

    for (BookModel *model in tags.mutArr) {
        NSLog(@"obj = %@",model);
    }

//#import “TagsModel.h”

@implementation TagsModel

- (instancetype)init
{
    self = [super init];
    if (self) {
    //实例化
        _mutArr = [[NSMutableArray alloc] init];
    }
    return self;
}

@end

数据模型test

//1. 根据URL,获取数据流

    NSData *data = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:LOCAL_P]];

//2. 查看json工具,json解析数据

    NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

//3.层层解析数据,存放到数据模型中

    ListModel *list = [[ListModel alloc] init];
    for (NSDictionary *dic2 in dic[@"List"]) {
        TaoBModel *model = [[TaoBModel alloc] init];

        model.nick = dic2[@"nick"];
        model.name = dic2[@"name"];
        model.price = dic2[@"price"];
        [list.mutArr addObject:model];
    }

    for (TaoBModel *model in list.mutArr) {
        NSLog(@"model = %@",model);
    }

//4. 归解档模型数据

    if([NSKeyedArchiver archiveRootObject:list.mutArr toFile:PATH])
    {
        NSLog(@"归档成功");
    }

    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithFile:PATH];
    for (TaoBModel *model in array) {
        NSLog(@"解档后:%@",model);
    }

//归解档到数据流中

    NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:list.mutArr];
    NSArray *array = [NSKeyedUnarchiver unarchiveObjectWithData:myData];
    for (TaoBModel *model in array) {
        NSLog(@"解档后:%@",model);
    }

//遵守协议

@interface TaoBModel : NSObject<NSCoding>

//实现协议方法

- (void)encodeWithCoder:(NSCoder *)coder
{
NSLog(@"进入归档");
[coder encodeObject:_nick forKey:@"nick"];
[coder encodeObject:_name forKey:@"name"];
[coder encodeObject:_price forKey:@"price"];
}

-(id)initWithCoder:(NSCoder *)aDecoder
{
NSLog(@"进入解档");
TaoBModel *model = [[TaoBModel alloc] init];
model.nick = [aDecoder decodeObjectForKey:@"nick"];
model.name = [aDecoder decodeObjectForKey:@"name"];
model.price = [aDecoder decodeObjectForKey:@"price"];
return model;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值