我真的很想尝试将一个JSON文件读入Swift,所以我可以玩它.我已经花了2天的时间重新搜索和尝试不同的方法,但没有运气,所以我已经注册StackOverFlow,看看是否有人可以指出我正确的方向.....
我的JSON文件名为test.json,包含以下内容:
{
"person":[
{
"name": "Bob",
"age": "16",
"employed": "No"
},
{
"name": "Vinny",
"age": "56",
"employed": "Yes"
}
]
}
Run Code Online (Sandbox Code Playgroud)
该文件直接存储在文档中,我使用以下代码访问它:
let file = "test.json"
let dirs : String[] = NSSearchPathForDirectoriesInDomains(
NSSearchpathDirectory.DocumentDirectory,
NSSearchPathDomainMask.AllDomainMask,
true) as String[]
if (dirs != nil) {
let directories: String[] = dirs
let dir = directories[0]
let path = dir.stringByAppendingPathComponent(file)
}
var jsonData = NSData(contentsOfFile:path, options: nil, error: nil)
println("jsonData \(jsonData)" // This prints what looks to be JSON encoded data.
var jsonDict = …Run Code Online (Sandbox Code Playgroud) 我的 xcode 项目中有一个 json 文件,我正在尝试通过以下方式读取该文件:
\n\nlet fileUrl = Bundle.main.url(forResource: "myfile", withExtension: "json")\nRun Code Online (Sandbox Code Playgroud)\n\n等等。
\n\n当我从项目目标成员资格内的文件(例如视图控制器文件)运行此代码时,代码将运行并且我能够读取 json。但是,当在我的 XCUITestCase 文件 \xe2\x80\x93 中运行它时,它失败了,无法找到 url。
\n\n如何从 XCUItest 中读取 json 文件?
\n\n我已经看过这篇文章,但没有帮助\n Read local JSON file in XCUITest
\n