swift3.0 存取json数据到沙盒

Posted on 2017-07-21 18:15  Apelike  阅读(1159)  评论(0编辑  收藏  举报

                    do {

                        //json保存到本地

                        let jsonData = try JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted)

                        // here "jsonData" is the dictionary encoded in JSON data

                        let data = jsonData as NSData

                        data.write(toFile: filePath, atomically: true)

                 

                        //构建文件路径

                        let filePath:String = NSHomeDirectory() + "/Documents/imagetovideo.json"

                        //读取json

                        var loaddata=NSData(contentsOfFile: filePath)

                        if loaddata != nil

                        {

                            let jData=loaddata as! Data

                            let decoded = try JSONSerialization.jsonObject(with: jData, options: [])

                            print("译码:",decoded)

                            

                        }

                    } catch {

                        print(error.localizedDescription)

                    }