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;
}