//
let array = [
["clsName": "WBHomeViewController",
"title": "首页",
"imageName": "home",
"visitorInfo": ["imageName": "",
"message": "关注一些人,回这里看看有什么惊喜"]],
["clsName": "WBMessageViewController",
"title": "消息",
"imageName": "message_center",
"visitorInfo": ["imageName": "visitordiscover_image_message",
"message": "登录后,别人评论你的微博,给你发消息,都会在这里收到通知"]],
["clsName": "UIViewController"],
["clsName": "WBDiscoverViewController",
"title": "发现",
"imageName": "discover",
"visitorInfo": ["imageName": "visitordiscover_image_message",
"message": "登录后,最新、最热微博尽在掌握,不再与实事潮流擦肩而过"]],
["clsName": "WBProfileViewController",
"title": "我",
"imageName": "profile",
"visitorInfo": ["imageName": "visitordiscover_image_profile",
"message": "登录后,你的微博、相册、个人资料会显示在这里,展示给别人"]]
]
// 写 json 方式一:
let os = OutputStream(toFileAtPath: "/Users/willokyes/Desktop/main.json",
append: false)
os?.open()
JSONSerialization.writeJSONObject(array,
to: os!,
options: JSONSerialization.WritingOptions.prettyPrinted,
error: NSErrorPointer.none)
os?.close()
// 写 json 方式二:
let data = try! JSONSerialization.data(withJSONObject: array,
options: JSONSerialization.WritingOptions.prettyPrinted)
let url = URL(fileURLWithPath: "/Users/willokyes/Desktop/main.json")
try! data.write(to: url, options: .atomic)
// 读 json:main.json 已拖放至 Xcode 项目 Bundle 里
guard let path = Bundle.main.path(forResource: "main.json", ofType: nil),
let data = NSData(contentsOfFile: path),
let array = try? JSONSerialization.jsonObject(with: data as Data) as? [[String: Any]]
else {
return
}
Swift 之读写 json 数据到文件
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...