国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

iOSJSON數(shù)據(jù)解析

2019-11-14 18:02:01
字體:
供稿:網(wǎng)友

JSON 是比較常用的數(shù)據(jù)格式,相比 xml 層次更清晰,這里介紹兩種解析 JSON 的方式:NSJSONSerialization 和 JSONKit

NSJSONSerialization 是 iOS 5 以后推出的,比較好用的 JSON 解析包.

JSON 數(shù)據(jù)格式由對(duì)應(yīng)的 '[',']' 和 '{','}',前者表示數(shù)組,后者表示字典.

NSJSONSerialization 解析過程:

1.獲取文件路徑

2.獲取文件內(nèi)容

3.解析

簡(jiǎn)單小例子:

 1 - (IBAction)parserJSON:(id)sender { 2      3     //獲取文件路徑 4      5     NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"json"]; 6     NSError *error = nil; 7     NSData *jsonData = [NSData dataWithContentsOfFile:jsonPath]; 8     NSMutableArray *array = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error]; 9     if (error == nil) {10         NSLog(@"%@",array);11     }else {12         NSLog(@"%@",error);13     }14     15     //數(shù)據(jù)封裝16     17     NSMutableArray *arr = [NSMutableArray array];18     19     for (NSDictionary *dic in array) {20         Student *stu = [[Student alloc]initWithDictionary:dic];21         [arr addObject:stu];22     }23     24     for (Student *stu in arr) {25         NSLog(@"%@",stu);26     }27 }

 

JSONKit 解析:(代碼)

 1 - (IBAction)parserJSONWithJESONKIT:(id)sender { 2      3     //獲取文件路徑 4      5     NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"Student" ofType:@"json"]; 6     NSError *error = nil; 7     NSString *JSONStr = [[NSString alloc]initWithContentsOfFile:jsonPath encoding:NSUTF8StringEncoding error:&error]; 8     NSLog(@"%@",JSONStr); 9     //讓jesonKIT 解析 JSON 數(shù)據(jù)10     NSMutableArray *array = [JSONStr objectFromJSONString];11     NSLog(@"%ld",array.count);12     //數(shù)據(jù)封裝13     NSMutableArray *arr = [NSMutableArray array];14     15     for (NSDictionary *dic in array) {16         Student *stu = [[Student alloc]initWithDictionary:dic];17         [arr addObject:stu];18     }19     20     for (Student *stu in arr) {21         NSLog(@"%@",stu);22     }23 }

JSONKit.zip 下載


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 旬邑县| 义马市| 长葛市| 万山特区| 东城区| 福海县| 会宁县| 城市| 永兴县| 瑞昌市| 潞西市| 上栗县| 固镇县| 舒城县| 建阳市| 屯留县| 阿图什市| 兴仁县| 蓬莱市| 民丰县| 德格县| 章丘市| 石阡县| 乌拉特前旗| 庆元县| 新蔡县| 渭源县| 大名县| 太原市| 手游| 两当县| 罗江县| 竹山县| 大同市| 萨迦县| 淮南市| 明光市| 吉水县| 寻乌县| 静安区| 孟州市|