ios -本地存储和查看json数据
2024-09-02 22:53:48
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"]);//遍历数组 }
最新文章
- 原创 C++应用程序在Windows下的编译、链接:第一部分 概述
- Qt中暂停线程的执行
- ORA-19502: write error on file ";xxxxx";, block number xxxx
- javascript中时间的手动创建date的方式
- Java Platform Standard Edition 8 Documentation
- Testing the CATCHER_DP
- easyui combo下拉框多选框
- STM32 常用GPIO操作函数记录
- highChartTable 切换
- iOS在UITableViewController里使用UISearchDisplayController报错";[UISearchResultsTableView dequeueReusableCellWithIdentifier:forIndexPath:]";
- JS 修改元素
- linux网络编程投票
- Mysql5.7 安装
- display:box;display:flex;弹性盒模型
- Delphi X10.2 + FireDAC 使用 SQL 语句 UPDATE
- react + antd 实现打印功能(踩了不少坑)
- Data Block Compression
- SSO单点登录三种情况的实现方式详解
- hdu2089_不要62
- impala系列:impala特有的操作符
热门文章
- 自己做的roguelike+恶魔城游戏《魔塔猎人》已发布。
- Laravel 5系列教程五:MVC的基本流程
- 【招聘App】—— React/Nodejs/MongoDB全栈项目:项目准备
- knowledgeroot 配置
- try....exception....finally
- 微信小程序 - 滑动显示地点信息(map)
- 首先给大家介绍一下数据库project师,数据库project师(Database Engineer),是从事管理和维护数据库管理系统(DBMS)
- openerp所用QWEB2的调试笔记
- js实现多少秒后自动跳转
- JS的同步加载、异步加载