零基础iOS之Json及XML数据解析
1 #pragma mark 1,json解析 2 //json解析 3 //1,获取文件路径 4 NSString *path = [[NSBundle mainBundle]pathForResource:@"Student" ofType:@"json"]; 5 6 //2,根据文件路径将其转成2进制数据 7 NSData *data = [NSData dataWithContentsOfFile:path]; 8 9 //3.将二进制数据转为对象类型数据 10 NSError *error = nil;//一个星号代表对象,两个星号代表对象取地址 11 NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; 12 13 NSMutableArray *arr = [NSMutableArray array];//存放stu模型的数组 14 15 for (NSDictionary *dic in array) { 16 17 Student *stu = [[Student alloc] init]; 18 [stu setValuesForKeysWithDictionary:dic];//字典转模型 19 20 [arr addObject:stu]; 21 } 22 NSLog(@"%ld",arr.count);
第三方json数据解析,首先引进JSONKit类
#pragma mark 2,第三方json解析 //1.获取文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"json"]; //2.转二进制 NSData *data = [NSData dataWithContentsOfFile:path]; //3.将二进制数据转化为对象 NSArray *array = [data objectFromJSONData]; //将数据和model类对应 NSMutableArray *arr = [NSMutableArray array]; for (NSDictionary *dic in array) { Student *stu = [[Student alloc] init]; [stu setValuesForKeysWithDictionary:dic]; [arr addObject:stu]; } NSLog(@"%ld",arr.count);
注意:系统与第三方区别,第三方对Json文件格式要求更加严格!
常用Json数据解析,XML步骤繁琐,不常用:
一、第三方(谷歌)
1.引进第三方GDataXMLNode
2.在build phases 里添加libxml2.dllib动态库
3.在build setting 里搜索 header search path 添加路径 /usr/include/libxml2
4.在build setting 里搜索 other link flag 添加 -lxml2
#pragma mark -XML之DOM解析 //1.获取文件路径 NSString *path = [[NSBundle mainBundle] pathForResource:@"Person" ofType:@"xml"]; //2.路径转化为字符串 NSString *str =[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; //3.将字符串转为xml类型数据 GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithXMLString:str options:0 error:nil]; //4.获取根节点 GDataXMLElement *root = [document rootElement]; //5.根据根节点去取数据 NSArray *arr = [root elementsForName:@"person"]; //6.对数组遍历 self.dataArr = [NSMutableArray array]; for (int i = 0; i < arr.count; i++) { Person *p = [[Person alloc] init]; NSArray *nameArr = [arr[i] elementsForName:@"name"]; p.name = nameArr[0]; NSArray *sexArr = [arr[i] elementsForName:@"sex"]; p.sex = sexArr[0]; NSArray *numberArr = [arr[i] elementsForName:@"number"]; p.number = numberArr[0]; NSArray *phoneArr = [arr[i] elementsForName:@"phone"]; p.phone = phoneArr[0]; [self.dataArr addObject:p]; } NSLog(@"%ld",self.dataArr.count);
二、系统自带方法 代理
#pragma mark - XML之SAX解析 //1.获取文件路径 self.dataArr = [NSMutableArray array]; NSString *path = [[NSBundle mainBundle] pathForResource:@"Person" ofType:@"xml"]; //2.根据文件路径转化为二进制数据 NSData *data = [NSData dataWithContentsOfFile:path]; //3.将二进制数据用系统NSXMLParser解析 NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data]; //4.设置代理 parser.delegate = self; //5.开始解析 [parser parse];
完成代理方法
//第一步,开始对document进行解析 - (void)parserDidStartDocument:(NSXMLParser *)parser{ NSLog(@"文本解析"); } //第二步,开始对节点解析 - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict{ NSLog(@"节点名%@",elementName); self.element = elementName; } //第三步,接收数据进行赋值 - (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{ NSLog(@"正在赋值"); NSLog(@"%@",string); if ([self.element isEqualToString:@"person"]) { self.person = [[Person alloc] init]; }if ([self.element isEqualToString:@"name"] && self.person.name == nil) { self.person.name = string; }if ([self.element isEqualToString:@"sex"] && self.person.sex == nil) { self.person.sex = string; }if ([self.element isEqualToString:@"number"] && self.person.number == nil) { self.person.number = string; }if ([self.element isEqualToString:@"phone"] && self.person.phone == nil) { self.person.phone = string; } } //第四步,完成节点解析 - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{ if ([elementName isEqualToString:@"person"]) { [self.dataArr addObject:self.person]; } NSLog(@"完成节点的解析"); } //第五步,完成文本解析 - (void)parserDidEndDocument:(NSXMLParser *)parser{ NSLog(@"%ld",self.dataArr.count); }