JSON解析 XML解析

2021-07-04 03:04:25 字數 1649 閱讀 7651

json解析

1大括號代表字典

2中括號(小括號)代表陣列

3冒號的左右兩邊分別為鍵和值(key:value)

4資料和資料之間

用逗號隔開

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

//json解析

本地讀取student.xm

l檔案nsstring *jstr=[nsstring stringwi

thconten

tsoffi

le:[[nsbundle mainbundle]pathforresource:@"file" oftype:@"json"] en

coding:nsutf8stri

ngencodi

ng 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 objectforke

y:@"age"];

//輸出取出的age

nslog(

@"%@",jage);

//xml解析

同樣我們也在本地建立乙個file.xml的檔案裡面包含:

kellen25m

jack35m

//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]ini

twithdata: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 i

n students)

}

JSON解析 XML解析

主要是顯示地理位置和天氣以及溫度。對網路請求下來的json資料進行解析。import viewcontroller.h define kweatherurl inte ce viewcontroller property weak,nonatomic iboutlet uilabel citylab...

UI高階 資料解析(XML解析和JSON解析)

ios資料解析主要有xml解析和json解析兩種方式,下面以本地資料的方式來說明這兩種方式 下面是一段簡單不完整的xml 草哥男24 好賤男25 你妹男40 每乙個xml檔案都有一組唯一根節點,根節點下有若干組子節點,每一組子節點又包含了三個自己的子節點 代表的是開始標籤,草哥 中間的草哥代表的是標...

XML檔案的解析和json的解析

解析就是從事先規定好的格式中提取資料 xml的解析方式有兩種 sax解析 dcm解析 安卓有乙個pull sax解析 nsstring path nsbundle mainbundle pathforresource message oftype txt nsdata data nsdata dat...