數據解析---常用的幾種解析方式


數據解析需要創建模型(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)只是第三步的解析方法不同而已。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM