博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
iOS 数据解析
阅读量:7085 次
发布时间:2019-06-28

本文共 1459 字,大约阅读时间需要 4 分钟。

hot3.png

JSON解析:

一、系统自带方法

- (IBAction)JsonSerialization:(id)sender {    //模拟构造数据    NSString *path = [[NSBundle mainBundle] pathForResource:@"Json" ofType:@"txt"];    NSData *data = [[NSData alloc]initWithContentsOfFile:path];    //使用系统提供的类进行json转换    //最方便的是字典和数组,NSJSONReadingAllowFragments,可以使用KVC    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingAllowFragments) error:nil];    //初始化数据    self.dataArray = [NSMutableArray array];    //遍历数组赋值    for (NSDictionary *dic in array) {        Person *p = [[Person alloc]init];        [p setValuesForKeysWithDictionary:dic];        [self.dataArray addObject:p];    }    //打印结果    for (Person *p in self.dataArray) {        NSLog(@"%@-%@-%@",p.name,p.sex,p.info);    }}

说明:Person 是一个model模型,self.dataArray 保存解析的数据。

二、JSONKit

- (IBAction)JSONKit:(id)sender {    //模拟数据    NSString *path = [[NSBundle mainBundle] pathForResource:@"Json.txt" ofType:nil];    NSData *data = [NSData dataWithContentsOfFile:path];    //使用第三方类库进行解析    NSArray *array = [data objectFromJSONData];    //初始化数据    self.dataArray = [NSMutableArray array];    //遍历数组赋值    for (NSDictionary *dic  in array) {        Person *p = [[Person alloc]init];        [p setValuesForKeysWithDictionary:dic];        [self.dataArray addObject:p];    }    //打印结果    for (Person *p in self.dataArray) {        NSLog(@"%@-%@-%@",p.name,p.sex,p.info);    }  }

说明:需要导入第三方类库JSONKit的头文件。

转载于:https://my.oschina.net/GeeksChen/blog/637073

你可能感兴趣的文章
Centos 6.8 系统下安装RabbitMQ方法
查看>>
SQL Server不能启动
查看>>
Educational Codeforces Round 65 (Rated for Div. 2) C. News Distribution
查看>>
[转] 如何写好.babelrc?Babel的presets和plugins配置解析
查看>>
The JVM Architecture Explained
查看>>
输入框禁止表情
查看>>
最大乘积(大佬的代码)
查看>>
dagger android 学习(四):基于dagger2的mvp架构
查看>>
CentOs7 使用iptables防火墙开启关闭端口
查看>>
12.29.作业
查看>>
项目管理初探
查看>>
keras入门--Mnist手写体识别
查看>>
animation渐进实现点点点等待效果实例页面
查看>>
配置 ssh无密码登陆
查看>>
java读取和写入浏览器Cookies
查看>>
熟悉常用的HDFS操作
查看>>
SCM软件配置管理 (一)SVN 与 CVS
查看>>
js闭包
查看>>
CocoaPods Setting up CocoaPods master repo无反应时的处理
查看>>
linux驱动系列之s3c2440内存布局
查看>>