解归档+json解析


 

一、归档解档

归档:对象的序列化,将对象按照某种格式存储到文件中

解档:对象的反序列化,从归档的文件得到对象的过程

 

1 官方类的归档解档

NSString/NSDictionary/NSArray/NSData等

(1)

//对象归档

        BOOLret = [NSKeyedArchiver archiveRootObject:dic toFile:@"/Users/qianfeng/Desktop/dic.arch"];

 

        //解档

        //方法1

        NSDictionary*dic = [NSKeyedUnarchiverunarchiveObjectWithFile:@"/Users/qianfeng/Desktop/dic.arch"];

 

        //方法2

        NSData*data = [NSDatadataWithContentsOfFile:@"/Users/qianfeng/Desktop/dic.arch"];

        dic = [NSKeyedUnarchiverunarchiveObjectWithData:data];

 

(2)多对象归档

//归档

NSMutableData *data =[NSMutableData data];

        //创建归档对象,归档的数据需要写入data

        NSKeyedArchiver *arch =[[NSKeyedArchiver alloc] initForWritingWithMutableData:data];

        //键值对形式存储归档数据

        [arch encodeObject:dic forKey:@"Dic"];

        [arch encodeInteger:age forKey:@"Age"];

        //结束归档          

        [arch finishEncoding];

       

        //将归档的数据写入文件

        [data writeToFile:@"/Users/qianfeng/Desktop/test.arch"atomically:YES];

 

//解档

NSData *data= [NSDatadataWithContentsOfFile:path];

        //创建解档对象

        NSKeyedUnarchiver*unarch = [[NSKeyedUnarchiveralloc] initForReadingWithData:data];

       

        //根据键值得到对应数据

        NSDictionary*dic = [unarchdecodeObjectForKey:@"Dic"];

        NSIntegerage = [unarchdecodeIntegerForKey:@"Age"];

        //解档结束

        [unarch finishDecoding];

 

(3)官方类遵守了NSCoding协议

@protocolNSCoding

 

- (void)encodeWithCoder:(NSCoder*)aCoder;

- (id)initWithCoder:(NSCoder*)aDecoder;// NS_DESIGNATED_INITIALIZER

 

@end

 

2 自定义类的归档解档

自定义类如果要实现归档和解档,需要遵守NSCoding协议

如果类中包含其他自定义类,那么也要遵守NSCoding协议

 

//归档时自动调用该方法

- (void)encodeWithCoder:(NSCoder*)aCoder{

    //键值对形式

    [aCoder encodeObject:self.carNameforKey:CAR];

    [aCoder encodeObject:self.myEngineforKey:ENGINE];

}

 

//解档时自动调用的方法

- (id)initWithCoder:(NSCoder*)aDecoder{

    //注意:如果父类遵守NSCoding协议,实现了initWithCoder方法,那么子类中需要调用[super initWithCode:]

    if (self = [superinit]) {

        self.carName = [aDecoderdecodeObjectForKey:CAR];

        self.myEngine = [aDecoderdecodeObjectForKey:ENGINE];

    }

    return self;

}

 

二、JSON

 

1、认识JSON

JSON:JavaScriptObject Notation

轻量级的数据交换格式

与语言/平台无关

 

数据在名称/值对中

数据由逗号分隔

{} 双括号表示对象

[] 中括号表示数组

 

//将对象转换为JSON格式的NSData对象

        NSData *data = [NSJSONSerializationdataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:nil];

        //写入文件

        [data writeToFile:@"/Users/qianfeng/Desktop/dic.json"atomically:YES];

        //通过NSFileManage写入文件

        NSFileManager *fm = [NSFileManagerdefaultManager];

        [fm createFileAtPath:@"/Users/qianfeng/Desktop/dic1.json"contents:data attributes:nil];

 

 

NSData*data = [NSDatadataWithContentsOfFile:@"/Users/qianfeng/Desktop/dic1.json"];

        //JSON格式的NSData对象解析出原对象

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

 

2、JSON数据解析

(1)本地数据解析

//读取json文件

        NSData*data = [NSDatadataWithContentsOfFile:@"/Users/qianfeng/Desktop/Book.json"];

        //转换为NSDictionary对象

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

 

(2)网络数据解析

//解析网络数据

        NSURL*url = [NSURLURLWithString:@"http://rr.local/1516/Book.json"];

        NSData*data = [NSDatadataWithContentsOfURL:url];

//转换为NSDictionary对象

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

 

3、数据模型

MVC:架构模式Model/View/Controller

Model一般包括数据结构和数据的操作

 


 

 

 

 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值