在 Xcode 中放置和处理 .json 文件的正确方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34493305/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Correct way to place and handle .json file in Xcode
提问by Jennifer
I just started to learn Swift and xcode and the first problem that I'm facing is how and where should I place the json file ? And how to use those files? Should I place the .json files within Assets folder ? Since I find it difficult, I would love to hear some tips or examples from you !
我刚开始学习 Swift 和 xcode,我面临的第一个问题是我应该如何以及在哪里放置 json 文件?以及如何使用这些文件?我应该将 .json 文件放在 Assets 文件夹中吗?因为我觉得很难,我很想听听你的一些提示或例子!
回答by Denis Kutlubaev
回答by Sukhdeep Singh Kalra
Please review the below image to check where to place the file.
I suggest you to create a group and add the file in that.
请查看下图以检查放置文件的位置。我建议您创建一个组并在其中添加文件。
After that, review the below could for using that file.
之后,查看以下可以使用该文件的方法。
Edit: This is the updated code for Swift 5 if that helps anyone.
编辑:如果对任何人有帮助,这是 Swift 5 的更新代码。
let path = Bundle.main.path(forResource: "filename", ofType: "json")
let jsonData = try? NSData(contentsOfFile: path!, options: NSData.ReadingOptions.mappedIfSafe)
回答by Henry Noon
How I've done this in September 2019...
我是如何在 2019 年 9 月做到这一点的...
1) In Xcode, create an Empty
file. Give the file a .json
suffix
1) 在 Xcode 中,创建一个Empty
文件。给文件一个.json
后缀
2) Type in or paste in your JSON
2) 输入或粘贴您的 JSON
3) Click Editor
-> Syntax Coloring
-> JSON
3) 点击Editor
-> Syntax Coloring
->JSON
4) Inside the file, highlight the JSON, click ctrl + i
to indent
4) 在文件中,突出显示 JSON,点击ctrl + i
缩进
5) import SwiftyJSON
using Cocoapods
5)import SwiftyJSON
使用 Cocoapods
6) In your ViewController, write...
6) 在你的 ViewController 中,写...
guard let path = Bundle.main.path(forResource: "File", ofType: "json") else { return }
let url = URL(fileURLWithPath: path)
do {
let data = try Data(contentsOf: url)
let json = try JSON(data: data)
} catch {
print(error)
}
N.B. - "File"
is the name of the file you created, but excluding the .json
suffix
NB -"File"
是您创建的文件的名称,但不包括.json
后缀
See SwiftyJSON GitHub page for more info - https://github.com/SwiftyJSON/SwiftyJSON
有关更多信息,请参阅 SwiftyJSON GitHub 页面 - https://github.com/SwiftyJSON/SwiftyJSON
回答by Abhooshan Bhattarai
var location = "test"
var fileType = "json"
if let path = Bundle.main.path(forResource: location, ofType: fileType) {
do {
let data = try Data(contentsOf: URL(fileURLWithPath: path), options: .alwaysMapped)
let jsonObj = JSON(data: data)
if jsonObj != JSON.null {
}
} catch let error {
print(error.localizedDescription)
}}
回答by Sukhdeep Singh Kalra
As per your requirement, you want to read json from that json file.
根据您的要求,您想从该 json 文件中读取 json。
I am using SWIFTY JSON Library for that.
我为此使用了 SWIFTY JSON 库。
Find below the link for that
找到下面的链接
https://github.com/SwiftyJSON/SwiftyJSON
https://github.com/SwiftyJSON/SwiftyJSON
Add this library to your project.
将此库添加到您的项目中。
After adding it, now review the below code:-
添加后,现在查看以下代码:-
let json = JSON(data: jsonData!)
for (index, subjson): (String, JSON) in json{
let value = subjson["key"].stringValue
}