iOS學習之資料解析

來源:互聯網
上載者:User

標籤:

解析:按照約定好的格式提取資料的過程叫做解析;後台開發人員按照約定好的格式存入資料,前端開發人員按照約定的格式讀取資料;主流的格式: XML / JSON 前端和後台都能識別的格式; XML解析XML解析的兩種工作原理:1.SAX解析:基於事件回調的解析機制,逐行進行解析,效率低,適合於大資料解析.  系統提供的NSXMLParser.2.DOM解析:把解析資料讀入記憶體,初始化產生樹形結構,逐層進行解析.效率高,適合於小資料解析. Google提供的GDataXMLNode.  解析工具:GDataXMLNode  是由Google提供的開源的基於C語言的libxml2.dylib動態連結程式庫而封裝的OC的XML解析類,效率比較高. 採用DOM解析的原理.使用GDataXML Nod步驟:1.target — Build Phases - Link Binary 添加libxml2.dylib2.target — Build Setting — 搜尋 Header Search path 添加 /usr/include/libxml2 libxml2.dylib 與 libxml2.2.dylib的區別:前者是一個捷徑,永遠指向的是最新的類庫,而後者才是真實的類庫,使用前者有一個好處,當類庫更新時,我們老版本的項目無需再重新匯入新的類庫.  JSON解析: JsonKit 是通過給NSString, NSData添加分類的形式,增加解析功能. 
//系統提供的解析JSON方法- (void)handleSystemParser:(UIBarButtonItem *)item {    //1.擷取檔案路徑    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"json"];    //2.根據檔案路徑初始化NSData對象    NSData *data = [NSData dataWithContentsOfFile:filePath];    //解析    NSArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];    NSLog(@"%@", arr);}

//第三方類庫解析JSON方法(這裡使用JSONKit)- (void)handleThirdPartParser:(UIBarButtonItem *)item { //將Json格式資料轉化為OC對象(NSString對象)

//1.擷取檔案路徑 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"json"]; //2.根據檔案路徑初始化字串對象 NSString *dataStr = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; //3.解析成OC對象.(可變對象/不可變對象) NSArray *arr1 = [dataStr objectFromJSONString]; NSArray *arr2 = [dataStr mutableObjectFromJSONString]; NSLog(@"%@",arr1); NSLog(@"%@", arr2);

  }

 

當然,也可以解析成NSData對象,第一步的方法都一樣,擷取檔案路徑

- (void)handleThirdPartParser:(UIBarButtonItem *)item {        //將Json格式資料轉化為OC對象(NSData對象)    //1.擷取檔案路徑    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"json"];     //根據檔案路徑初始化成NSData對象     NSData *data = [NSData dataWithContentsOfFile:filePath];     //解析.(可變/不可變)     NSArray *arr1 = [data objectFromJSONData];     NSMutableData *arr2 = [data mutableObjectFromJSONData];

 

以上是將JSON資料轉為OC的對象,下面是講OC對象轉為JSON資料的方法.

    //將OC對象轉成JSON格式資料.    //1.將OC的數組對象轉成JSON格式資料.    NSArray *arr1 = @[@"aa",@"bb",@"cc",@"dd"];    NSData *jsonData1 = [arr1 JSONData];//轉成JSON格式Data    NSString *jsonStr1 = [arr1 JSONString]; //轉成JSON格式String        //2.將OC的字典對象轉成JSON格式資料.    NSDictionary *dic2 = @{@"name":@"Frank",                           @"age":@"18"                           };    NSData *jsonData2 = [dic2 JSONData];//轉成JSON格式Data    NSString *jsonStr2 = [dic2 JSONString];//轉成JSON格式String

 

 

 

 

iOS學習之資料解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.