解析
按照约定好的格式提取出来数据的过程
主流的两种格式 ✓XML ✓JSON
XML:Extensible Markup language (可扩展标记语言) 用来存储和传输数据。解析工具包括 SAX DOM
解析的工作原理:
1,SAX (Simple API for XML) 解析:基于事件的回调解析机制。逐行解析数据,效率低,适用于大数据的解析。
NSXMLParser是XML自带的类,采用SAX解析。解析过程由NSXMLParserDelegate回调;解析过程:开始标签-》取值-》结束标签-》取值
2,DOM (Document Object Modol 文档对象模型)解析:把数据读入内存,一次性读取数据,已树形结构,逐层解析,效率高,适用于小数据的解析。 DOM 解析: 会将全部的内容一次性全部读入,把数据都城一个树形结构,按照树形结构的层级来取内容
使用的是GDataXMLNode:由谷歌提供的一个在C语言libxml2.dylib 动态链接库基础上封装成的oc 的一个数据解析类。它的解析效率比SAX高将近10倍。
1.target -- Build setting -- Header Search Path 添加/usr/include/libxml2
使用的是GDataXMLNode:由谷歌提供的一个在C语言libxml2.dylib 动态链接库基础上封装成的oc 的一个数据解析类。它的解析效率比SAX高将近10倍。
1.target -- Build setting -- Header Search Path 添加/usr/include/libxml2
2.target -- Build Phases --Link Binary 添加 libxml2.dylib动态链接库
GDataXMLNode是谷歌提供的开源XML解析类,采用DOM方式解析数据。
JSON :Javascript Object Notation,轻量级的数据交换格式,采用完全独立于语言的文本格式,被称为理想的数据交换语言。
JSON 有两种结构:对象 数据
JSON的数据类型:字符串、数值、BOOL、对象、数组。
@property (nonatomic,retain)NSMutableArray *dataSource;//存储数据源
- (NSMutableArray *) dataSource {
if (!_dataSource) {
self
.dataSource = [NSMutableArray array];
}
return _dataSource;
}
//*****系统解析
- (IBAction)handleSystemJsonParser:(id)sender {
//清除数据
[self.dataSourse removeAllObjects];
//1.获取本地文件
NSString *file = [[NSBundle mainBundle]pathForResource:@"Student" ofType:@"json"];
//2.根据文件路径初始化为NSData对象
NSData *data = [NSData dataWithContentsOfFile:file];
//3.解析
NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dic in arr) {
Student *stu = [[Student alloc]init];
[stu setValuesForKeysWithDictionary:dic];
[self.dataSourse addObject:stu];
[stu release];
}
//刷新数据
[self.tableView reloadData];
- (IBAction)handleSystemJsonParser:(id)sender {
//清除数据
[self.dataSourse removeAllObjects];
//1.获取本地文件
NSString *file = [[NSBundle mainBundle]pathForResource:@"Student" ofType:@"json"];
//2.根据文件路径初始化为NSData对象
NSData *data = [NSData dataWithContentsOfFile:file];
//3.解析
NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dic in arr) {
Student *stu = [[Student alloc]init];
[stu setValuesForKeysWithDictionary:dic];
[self.dataSourse addObject:stu];
[stu release];
}
//刷新数据
[self.tableView reloadData];
}
//第二种方式
//2.根据路劲转为NSData对象
NSData *data = [NSData dataWithContentsOfFile:file];
//3.解析
NSArray *arr = [data objectFromJSONData];//不可变对象
NSMutableArray *mutArr = [data mutableObjectFromJSONData];//可变对象
for (NSDictionary *dic in arr) {
Student *stu = [[Student alloc]init];
[stu setValuesForKeysWithDictionary:dic];
[self.dataSourse addObject:stu];
[stu release];
}
//刷新数据
//2.根据路劲转为NSData对象
NSData *data = [NSData dataWithContentsOfFile:file];
//3.解析
NSArray *arr = [data objectFromJSONData];//不可变对象
NSMutableArray *mutArr = [data mutableObjectFromJSONData];//可变对象
for (NSDictionary *dic in arr) {
Student *stu = [[Student alloc]init];
[stu setValuesForKeysWithDictionary:dic];
[self.dataSourse addObject:stu];
[stu release];
}
//刷新数据
[self.tableView reloadData];
//将oc对象转为JSON格式的数据
NSArray *array = @[@"11",@"22",@"33"];
NSData *jsondata = [array JSONData];//转化为JSON格式的Data;
NSString *str = @"testString";
NSString *jsonStr = [str JSONString];//转化为JSON格式的string
NSDictionary *dic = @{@"name":@"夏凉",@"age":@"25"};
NSData *data1 = [dic JSONData];
}
NSArray *array = @[@"11",@"22",@"33"];
NSData *jsondata = [array JSONData];//转化为JSON格式的Data;
NSString *str = @"testString";
NSString *jsonStr = [str JSONString];//转化为JSON格式的string
NSDictionary *dic = @{@"name":@"夏凉",@"age":@"25"};
NSData *data1 = [dic JSONData];
}