如何解析本地文件(iOS学习)
2015-10-09 10:36
375 查看
//首先本地有个json文件,名为Data.json,然后给解析创建一个setData方法
-(void)setData{
//1.获取文件路径
NSString *filePath = [[NSBundle
mainBundle]pathForResource:@"Data"
ofType:@"json"];
//2.转化为data
NSData *data =[NSData
dataWithContentsOfFile:filePath];
//3.json解析
NSArray *rootArray = [NSJSONSerialization
JSONObjectWithData:data
options:0 error:nil];
self.dataArray = [NSMutableArray
array];
for (NSDictionary *dic
in rootArray) {
//给model层初始化,把解析的字典整体放到model里,model里存了一层一层的数据,放到_dataArray数组里
Model *model = [[Model
alloc] init];
[model setValuesForKeysWithDictionary:dic];
[_dataArray
addObject:model];
}
}
-(void)setData{
//1.获取文件路径
NSString *filePath = [[NSBundle
mainBundle]pathForResource:@"Data"
ofType:@"json"];
//2.转化为data
NSData *data =[NSData
dataWithContentsOfFile:filePath];
//3.json解析
NSArray *rootArray = [NSJSONSerialization
JSONObjectWithData:data
options:0 error:nil];
self.dataArray = [NSMutableArray
array];
for (NSDictionary *dic
in rootArray) {
//给model层初始化,把解析的字典整体放到model里,model里存了一层一层的数据,放到_dataArray数组里
Model *model = [[Model
alloc] init];
[model setValuesForKeysWithDictionary:dic];
[_dataArray
addObject:model];
}
}
相关文章推荐
- iOS runtime运行时机制
- iOS开发者账号配置进行设备调试
- ios 开发日记 15 - 上传报错90535
- iOS9 让开发者蛋疼的一些坑总结
- iOS之PC与MAC虚拟机之间的文件传输
- IOS 异步GET方法请求
- [IOS]edgesForExtendedLayout、automaticallyAdjustsScrollViewInsets
- 判断iOS设备的尺寸
- iOS开发中ViewController的页面跳转和弹出模态
- ios 操作日期NSDate
- IOS设备判断
- IOS 移除cell或者视图中的子视图
- iOS 更改项目名
- iOS中 数据持久化的方法
- iOS中 数据持久化方式
- iOS开发中coredata的使用和数据持有化
- iOS分析远程推送的步骤
- iOS 8 新特性介绍
- 【投稿】iOS 9:快速让你的APP支持spotlight搜索
- iOS开发-常用第三方开源框架介绍