我有這個JSON文件:當我嘗試初始化它解析本地JSON文件
static func parseJson(file: String) -> [String: Any] {
let jsonFile = Bundle.main.path(forResource: file, ofType: "json")
let jsonData = NSData(contentsOfFile: jsonFile!)
let jsonDictionary = try? JSONSerialization.jsonObject(with: jsonData! as Data, options: [])
return jsonDictionary as! [String: Any] <-- Error
}
然後:
{
"People" : [{
{
"Name": "Jack"
"Age": 130,
"Job": "Doctor",
},
{
"Name": "Mark"
"Age": 45,
"Job": "Engineer",
},
{
"Name": "Sarah"
"Age": 27,
"Job": "Designer",
},
}]
}
這是我如何解析使用一個輔助函數它
let data = parseJson(file: "People") as! [String: Any]
print(data["people"])
我收到此錯誤:
fatal error: unexpectedly found nil while unwrapping an Optional value
我在這裏做錯了什麼?我跟着這個tutorial by Apple
jsonlint.com說無效JSON –