一.JSON解析:(创建一个Teacher类)
1.第一种方式:
//获取文件路径
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Teacher" ofType:@"txt"];
//读取文件中的数据,将文件中的数据转换成NSData
NSData *data = [NSData dataWithContentsOfFile:filePath];
#pragma mark------解析JSON数据第一种方法
NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSLog(@"array = %@",array);
//给老师数组开辟空间
self.teachersArray = [NSMutableArray array];
//遍历数组
for (NSDictionary *dic in array) {
//创建teacher对象
Teacher *teacher = [[Teacher alloc]init];
//给teacher中的属性赋值
[teacher setValuesForKeysWithDictionary:dic];
//添加
[_teachersArray addObject:teacher];
[teacher release];
}
//JSON解析结束
for (Teacher *teacher in _teachersArray) {
NSLog(@"%@",teacher);
}
2.第二种方式:用第三方JSONKit
//获取文件路径
NSString *filePath = [[NSBundle mainBundle]pathForResource:@"Teacher" ofType:@"txt"];
//读取文件中的数据,将文件中的数据转换成NSData
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSMutableArray *array1 = [data objectFromJSONData];
NSLog(@"array1 = %@",array1);
NSMutableDictionary *dict = [data objectFromJSONData];
NSLog(@"dict = %@",dict);
//开辟空间
self.teacherDic = [[NSMutableDictionary alloc]init];
self.array1 = [NSMutableArray arrayWithArray:[dict allKeys]];
//遍历字典
for (NSString *key in dict) {
//创建数组
NSLog(@"+++++%@",_array1);
NSMutableArray *array2 =[NSMutableArray array];
[array2 addObject:array1];
for (NSDictionary *dic2 in dict[key]) {
Teacher *teacher = [[Teacher alloc]init];
[teacher setValuesForKeysWithDictionary:dic2];
[_teachersArray addObject:teacher];
[teacher release];
}
for (Teacher *teacher in _teachersArray) {
NSLog(@"****%@",teacher);
}
}
//将NSDictionary类型转换成NSString类型
NSDictionary *dict = @{@"key":@[@"A",@"B",@"C",@"D"],@"key1":@[@"E",@"F",@"G",@"H"]};
NSString * string = [dict JSONString];
NSLog(@"dict = %@",string);
//将NSData类型转换成NSDictionary类型
NSData * data1 = [dict JSONData];
NSDictionary *dic = [data1 objectFromJSONData];
NSLog(@"dic = %@",dic);
//将NSData转换成NSStrig类型
NSString * string1 = [[NSString alloc]initWithData:data1 encoding:0];
NSLog(@"string1 = %@",string1);
//将NSString转换成NSData类型
NSData *data2 = [string1 JSONData];
NSLog(@"data2 = %@",data2);
二.DOM方式解析:(创建一个Student类)
//获取文件路径
NSString * filePath = [[NSBundle mainBundle]pathForResource:@"Student" ofType:@".txt"];
//读取文件中的数据
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSLog(@"%@",data);
//解析数据
NSError *error = nil;
//①.创建解析数据的工具类对象
//当创建完工具类对象之后,会把整个data数据读入内存,然后帮你生成树状结构,存放在工具类对象里
GdataxMLDocument *xmlDocument =[[GdataxMLDocument alloc]initWithData:data options:0 error:&error];
//开始解析
//(1).获取根节点
GdataxMLElement * rootElement = xmlDocument.rootElement;
//给学生数组开辟空间
self.studentsArray = [NSMutableArray array];
//(2).获取根节点下的所有子节点
for (GdataxMLElement * stuElement in rootElement.children) {
//根节点中有几个节点,就创建几个学生对象
Student *stu = [[Student alloc]init];
//遍历子节点
for (GdataxMLElement *subElement in stuElement.children) {
//使用KVC给每个对象中的属性赋值
[stu setValue:subElement.stringValue forKey:subElement.name];
}
[self.studentsArray addObject:stu];
[stu release];
}
//DOM解析结束
for (Student *stu in _studentsArray) {
NSLog(@"DOM解析:%@",stu);
}
#pragma mark-----
//把所有标签名为name的节点放入数组(第一个斜杠是第一层,两个斜杠是第二层,根节点不算)
NSArray *nameArray = [xmlDocument nodesForXPath:@"//name" error:nil];
//获取所有学生的姓名
for (GdataxMLElement *element in nameArray) {
NSLog(@"名字:%@",element.stringValue);
}
//获取所有学生的学号
NSArray *numberArray = [xmlDocument nodesForXPath:@"//number" error:nil];
for (GdataxMLElement *numberelement in numberArray) {
NSLog(@"学号:%@",numberelement.stringValue);
}
三.Sax方式解析:
//获取文件路径
NSString *filePath =[[NSBundle mainBundle]pathForResource:@"Student" ofType:@".txt"];
//读取文件中的数据
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSLog(@"%@",data);
//解析数据
//①.创建解析数据的工具类对象
NSXMLParser *parser = [[NSXMLParser alloc]initWithData:data];
//②.设置代理,因为NSXMLParser这个类的解析是基于协议回调机制的
parser.delegate = self;
//③.手动开启解析
[parser parse];
注意:Sax方式必须实现的五步:
//1.开始解析文档
- (void)parserDidStartDocument:(NSXMLParser *)parser
{
//初始化学生数组
self.studentsArray = [NSMutableArray array];
}
//2.解析开始标签
-(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
_currentElementName = elementName;
//打印开始标签
NSLog(@"***%@",elementName);
if ([elementName isEqualToString:@"stu"]) {
Student *stu = [[Student alloc]init];
[_studentsArray addObject:stu];
[stu release];
}
}
//3.取出标签后的值
-(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
//获取刚刚添加到数组中新的元素(最后一个元素)
Student * stu = [_studentsArray lastObject];
//KVC方式给属性赋值
[stu setValue:string forKey:_currentElementName];
//打印标签后的值
NSLog(@"%@",string);
}
//4.解析结束标签
-(void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
NSLog(@"+++%@",elementName);
//避免给同一个属性,重新赋值,这个步骤必须有
_currentElementName = nil;
}
//5.结束解析文档
- (void)parserDidEndDocument:(NSXMLParser *)parser
{
NSLog(@"结束解析文档");
//遍历一下学生数组
for (Student *stu in _studentsArray) {
NSLog(@"%@",stu);
}
}
展示一下Teacher.txt里的内容
{"key1":
[ {"number":"1","name":"张三","sex":"男","age":"20"},{"number":"2","name":"李四","sex":"女","age":"23"},{"number":"3","name":"王五","age":"21"} ],"key2":
[ {"number":"4","name":"三",{"number":"5","name":"四",{"number":"6","name":"五","age":"21"} ] }
展示一下Student.txt里的内容
<?xml version="1.0" encoding="UTF-8"?>
<students>
<stu>
<number>1</number>
<name>张三</name>
<time>今晚8点</time>
<address>如家403</address>
</stu>
<stu>
<number>2</number>
<name>李四</name>
<time>今晚9点</time>
<address>如家404</address>
</stu>
<stu>
<number>3</number>
<name>王五</name>
<time>今晚10点</time>
<address>如家405</address>
</stu>
</students>