数据解析---常用的几种解析方式

数据解析需要创建模型(model),然后使用KVC赋值:注意模型的.h文件中声明的属性必须跟要解析的数据中的“key”相同,数量上可以不同但要在.m文件中加入方崩溃语句://KVC赋值防止崩溃的方法
- (void)setValue:(id)value forUndefinedKey:(NSString *)key {   
}

一、本地plist文件:

  //1.获取文件路径//参数1写plist文件的文件名,参数2写后缀;也可以在参数1处写全称加后缀,然后在参数2处写nil;注意写的时候一个字母都不能错。
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Contacts" ofType:@"plist"];
    //2.根据路径获取数据//plist文件中最外层的文件是什么类型就用什么类型接收;本次举例的plist文件的最外层是数组就定义NSArray接收
    NSArray *dataArray = [NSArray arrayWithContentsOfFile:filePath];
    //3.遍历数组,将数组中的数据转为model对象//根据plist中文件的具体内容解析,使用KVC赋值
    for (NSDictionary *dict in dataArray) {
        Person *person = [[Person alloc] init];
        [person setValuesForKeysWithDictionary:dict];//KVC赋值

//创建可变数组接收多个模型

[self.dataArray addObject:stu];//self.dataArray必须先在前面初始化才能使用
}

二、根据被解析数据的格式可以分为MXL文件和JSON文件;其中XML文件使用SAX解析和DOM解析,下面只介绍DOM解析(使用第三方GDataXMLNode):

首先连接动态库libxml2.tbd,并引入第三方的头文件

//1.获取文件的路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"StudentInfo_xml" ofType:@"txt"];
    //2.根据路径获取data数据
    NSData *data = [NSData dataWithContentsOfFile:path];
    //2.5初始化存储数据的数组
    self.dataArray = [NSMutableArray array];
    //3.设置DOM解析(创建解析文档)
    GDataXMLDocument *document = [[GDataXMLDocument alloc] initWithData:data options:0 error:nil];
    //4.获取根节点
    GDataXMLElement *rootElement = document.rootElement;
    //5.遍历获取相对应的子节点
    for (GDataXMLElement *studentElement in rootElement.children) {
        Student *stu = [[Student alloc] init];
        //遍历子节点的子节点
        for (GDataXMLElement *stuElement in studentElement.children) {
            NSLog(@"stuElement = %@", stuElement);
            //stuElement.name 标签的名字
            //stuElement.stringValue标签的值
            //KVC赋值
            [stu setValue:stuElement.stringValue forKey:stuElement.name];
        }
        [self.dataArray addObject:stu];
    }
    //遍历检验
    for (Student *stu in self.dataArray) {
        NSLog(@"name = %@, gender = %@, age = %ld, hobby = %@", stu.name, stu.gender, stu.age, stu.hobby);
    }
JSON文件的解析可以使用系统自带方法也可以使用第三方,首先介绍使用系统自带方法:

 //1.获取文件路径
    NSString *path = [[NSBundle mainBundle] pathForResource:@"StudentInfo_json" ofType:@"txt"];
    //2.根据路径获取data数据     必须使用data数据接收因为JSON解析的数据类型是NSData类型
    NSData *data = [NSData dataWithContentsOfFile:path];
    //2.5初始化存储数据的数组
    self.dataArray = [NSMutableArray array];
    //3.解析
    NSArray *resultArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
   //4.遍历数组,使用KVC给对象赋值
    for (NSDictionary *dic in resultArray) {
        Student *stu = [[Student alloc] init];
        //将字典中的值赋值给对象
        [stu setValuesForKeysWithDictionary:dic];
        //将对象添加到数组中
        [self.dataArray addObject:stu];
    }
    //遍历检验
    for (Student *stu in self.dataArray) {
        NSLog(@"name = %@, gender = %@, age = %ld, hobby = %@", stu.name, stu.gender, stu.age, stu.hobby);
    }
//使用第三方(如:JSONKit)只是第三步的解析方法不同而已。

原文地址:https://www.cnblogs.com/bdlfbj/p/5491107.html