swift3 保存json到桌面,从沙盒中读取json
来源:互联网 发布:淘宝烟丝 编辑:程序博客网 时间:2024/06/05 15:58
// 获得本地的沙盒路径
let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] let jsonPath = (docDir as NSString).appendingPathComponent("main.json") // 在本地的沙盒路径下写入data数据 data?.write(toFile: jsonPath, atomically: true)
注意:在json文件中格式必须正确,否则转为JSON文件会出错,并且在JSON中”{}”表示”[]”
从沙盒中读取json文件
// 0. 获取沙盒 json 路径 let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] let jsonPath = (docDir as NSString).appendingPathComponent("main.json") // 加载data var data = NSData(contentsOfFile: jsonPath) // 判断 data路径是否有内容,如果没有内容则本地沙盒没有文件 , 需要加载默认的json if data == nil { // 从bundle 加载data let path = Bundle.main.path(forResource: "main.json", ofType: nil) data = NSData(contentsOfFile: path!) } // data一定会有一个内容 , 这时候 需要反序列化json 来读取 数据内容 //反序列化转换成数组 guard let array = try? JSONSerialization.jsonObject(with: data! as Data, options: []) as? [[String : Any]] else{ return }
阅读全文
0 0
- swift3 保存json到桌面,从沙盒中读取json
- swift3读取本地json数据 --JSONSerialization.jsonObject()
- android .向sdcard写入json字符串、读取json字符保存到list<类>中供调用
- VB.NET 从XML文件中读取内容到JSON
- python-从文件读取json数据写到数据库
- 从JSON中读取数据追加到HTML中
- 从json到gson
- 从本地读取json文件
- json文件的读取与保存
- Qt之JSON保存与读取
- Python——保存、读取json文件
- qt 之json 保存与读取
- java 保存和读取本地json文件
- 用python读取json格式内容并保存到excel中
- 在c++中使用json----保存到文件、从文件中解析
- 在c++中使用json----保存到文件、从文件中解析
- pyhton 从web获取json数据 保存到本地然后再读取
- 在c++中使用json----保存到文件、从文件中解析
- 每天一个 Linux 命令(32):gzip命令
- 数组排序sort()
- AndroidStudio本地化配置gradle的buildToolsVersion和gradleBuildTools
- 轮播图--文字上下方向自动滑动
- 第4章C/C++语言基础 4.3~4.6
- swift3 保存json到桌面,从沙盒中读取json
- 4836: [Lydsy2017年4月月赛]二元运算
- 离散题目8
- Spring的自动装配
- Java多线程之内存可见性
- 10、Linux根文件系统的制作
- HDU 1241 Oil Deposits
- 算法导论 练习题 15.3-1
- 数据库设计(降龙十四招式)