1.代码创建json文件,并保存到本地
 
第一步.设置json文件的保存路径
NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/myJson.json"];

NSLog(@"%@",filePath);
第二步.准备存储数据

 

NSMutableArray *arr = [[NSMutableArray alloc]init]; //用来盛放数据的value

NSDictionary *dic = @{@"key1":@"value1",@"key2":@"value2",@"key3":@"value3",@"key4":@"value4"};
NSDictionary *dic1 = @{@"key1":@"value1",@"key2":@"value2",@"key3":@"value3",@"key4":@"value4"};
NSDictionary *dic2 = @{@"key1":@"value1",@"key2":@"value2",@"key3":@"value3",@"key4":@"value4"}; [arr addObjectsFromArray:@[dic,dic1,dic2]]; NSDictionary *json_dic = @{@"arr":arr};//key为arr value为arr数组的字典
第三步.封包数据

 

NSData *json_data = [NSJSONSerialization dataWithJSONObject:json_dic options:NSJSONWritingPrettyPrinted error:nil];
第四步.写入数据

 

[json_data writeToFile:filePath atomically:YES];
经过这四步,就在本地指定路径filePath创建了一个json文件。(根目录是一个字典 key为arr value为arr数组的字典

 

2.读取本地json数据

 

NSString *filePath = [NSHomeDirectory() stringByAppendingString:@"/Documents/myJson.json"];//获取json文件保存的路径

NSData *data = [NSData dataWithContentsOfFile:filePath];//获取指定路径的data文件

id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil]; //获取到json文件的跟数据(字典)

NSArray *arr = [json objectForKey:@"arr”];//获取指定key值的value,是一个数组

for (NSDictionary *dic in arr) {

    NSLog(@"%@",[dic objectForKey:@"key1"]);//遍历数组

}  

最新文章

  1. 原创 C++应用程序在Windows下的编译、链接:第一部分 概述
  2. Qt中暂停线程的执行
  3. ORA-19502: write error on file "xxxxx", block number xxxx
  4. javascript中时间的手动创建date的方式
  5. Java Platform Standard Edition 8 Documentation
  6. Testing the CATCHER_DP
  7. easyui combo下拉框多选框
  8. STM32 常用GPIO操作函数记录
  9. highChartTable 切换
  10. iOS在UITableViewController里使用UISearchDisplayController报错"[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:]"
  11. JS 修改元素
  12. linux网络编程投票
  13. Mysql5.7 安装
  14. display:box;display:flex;弹性盒模型
  15. Delphi X10.2 + FireDAC 使用 SQL 语句 UPDATE
  16. react + antd 实现打印功能(踩了不少坑)
  17. Data Block Compression
  18. SSO单点登录三种情况的实现方式详解
  19. hdu2089_不要62
  20. impala系列:impala特有的操作符

热门文章

  1. 自己做的roguelike+恶魔城游戏《魔塔猎人》已发布。
  2. Laravel 5系列教程五:MVC的基本流程
  3. 【招聘App】—— React/Nodejs/MongoDB全栈项目:项目准备
  4. knowledgeroot 配置
  5. try....exception....finally
  6. 微信小程序 - 滑动显示地点信息(map)
  7. 首先给大家介绍一下数据库project师,数据库project师(Database Engineer),是从事管理和维护数据库管理系统(DBMS)
  8. openerp所用QWEB2的调试笔记
  9. js实现多少秒后自动跳转
  10. JS的同步加载、异步加载