JSON解析和XML解析

來源:互聯網
上載者:User

標籤:blog   http   io   os   ar   使用   for   sp   檔案   

一、 XML:用到一個開源解析類,GDataXMLNode(將其加入項目中),添加libxml2.dylib架構

        經常用到的方法:

1.- (id)initWithXMLString:(NSString *)str options:(unsigned int)mask error:(NSError *)error

2.- (id)initWithData:(NSData *)data options:(unsigned int)mask error:(NSError *)error

這兩個方法可以將NSString類型或者NSData類的資料轉化為GDataXMLNode類可以解析的XML文檔 

  3.- (GDataXMLElement *)rootElement 返回GDataXMLElement中所有節點資訊

4.- (NSArray *)elementsForName:(NSString *)name   返回當前節點下名稱為name的所有節點,傳回值為數組。

5.- (NSString *)stringValue 返回節點中間的值。

 

二、 JSON:用到開源的架構SBJSON,對NSString類的擴充,我們程式中的所有NSString對象都可以調用其中的方法。

 SBJSON的方法- (id)JSONValue 此方法返回一個ID類對象,因為JSON資訊都是以索引值來傳輸的,所以我們會用NSDictionary來接收這個傳回值。

 

兩種結構:

1.“索引值對”的集合。對象(object),記錄(Record),結構(struct),字典(dictionary),雜湊表(hashtable),鍵列表(keyed list),關聯陣列(associative array)。

2.值的有序列表。數組(Array)。 

 

 

 

JSON解析
1大括弧代表字典
2中括弧(小括弧)代表數組
3冒號的左右兩邊分別為鍵和值(key:value)
4資料和資料之間用逗號隔開

我們在本機上建立一個file.xml,進行解析,並取出年齡35


//JSON解析
   本地讀取student.xml檔案
    NSString *jstr=[NSString stringWithContentsOfFile:[[NSBundle mainBundle]pathForResource:@"File" ofType:@"json"] encoding:NSUTF8StringEncoding error:nil];//取到路徑
    //進行解析,因為最外面是字典,所以要用字典類型的對象來接
    NSDictionary *dic=[jstr JSONFragmentValue];
    //下面取值
    //取到hello這個key,取到的是數組裡的內容所以要用數組來接
    NSArray *arr=[dic objectForKey:@"hello"];
    //取到下標為1的數組,因為數組裡面又是一個字典,所以用字典來接
    NSDictionary *dic2=[arr objectAtIndex:1];
   //取到字典key為age的value,因為取出來的值是字串,用字串來接
    NSString *jage=[dic2 objectForKey:@"age"];
    //輸出取出的age
    NSLog(@"%@",jage);
//XML解析
同樣我們也在本地建立一個File.xml的檔案裡麵包含:
<students>
    <student>
        <name>kellen</name>
        <age>25</age>
        <sex>M</sex>
    </student>
    <student>
        <name>jack</name>
        <age>35</age>
        <sex>M</sex>
    </student>
</students>

    //step1:本地讀取student.xml檔案
    //擷取目錄的xml檔案
    NSString*xmlPath=[[NSBundle mainBundle]pathForResource:@"File" ofType:@"xml"];
    NSData*xmlData=[[NSData alloc]initWithContentsOfFile:xmlPath];
    //step2:將包含xml內容的字串轉化為document對象
   
    GDataXMLDocument*xmldoc=[[GDataXMLDocument alloc]initWithData:xmlData options:0 error:nil];
   
    //step3:擷取到根節點
    //擷取根節點(students)
    GDataXMLElement*rootElement=[xmldoc rootElement];
   
    //step4:遍曆節點資訊
    //擷取根節點下的節點(student)
    NSArray*students=[rootElement elementsForName:@"student"];
    NSLog(@"%@",students);
    //使用forin遍曆XML中的所有資料
    for (GDataXMLElement*stu in students) {
        //擷取name節點的屬性
        GDataXMLElement*nameElement=[[stu elementsForName:@"name"]objectAtIndex:0];
        NSString*name=[nameElement stringValue];//擷取nameElement
        NSLog(@"Student name is:%@",name);
        //擷取age節點的屬性
        GDataXMLElement*ageElement=[[stu elementsForName:@"age"]objectAtIndex:0];
        NSString*age=[ageElement stringValue];
        NSLog(@"User age is :%@",age);
    }
}

JSON解析和XML解析

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.