使用Swift 3读取JSON文件

时间:2016-11-05 13:28:10

标签: json swift3

我有一个名为 points.json 的JSON文件,以及一个读取函数,如:

private func readJson() {
    let file = Bundle.main.path(forResource: "points", ofType: "json")
    let data = try? Data(contentsOf: URL(fileURLWithPath: file!))
    let jsonData = try? JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]
    print(jsonData)
}

它不起作用,有什么帮助?

2 个答案:

答案 0 :(得分:95)

这里的问题是你强制打开值,如果出现错误,你就无法知道它的来源。

相反,您应该处理错误并安全地打开您的选项。

正如@vadian在评论中正确指出的那样,你应该使用Bundle.main.url

private func readJson() {
    do {
        if let file = Bundle.main.url(forResource: "points", withExtension: "json") {
            let data = try Data(contentsOf: file)
            let json = try JSONSerialization.jsonObject(with: data, options: [])
            if let object = json as? [String: Any] {
                // json is a dictionary
                print(object)
            } else if let object = json as? [Any] {
                // json is an array
                print(object)
            } else {
                print("JSON is invalid")
            }
        } else {
            print("no file")
        }
    } catch {
        print(error.localizedDescription)
    }
}

在Swift编码时,!通常是代码气味。当然有例外(IBOutlets和其他人)但是尝试不使用!自己强行解包并且总是安全地解包。

答案 1 :(得分:1)

下面的Swift 4.2 / iOS 12代码显示了方法的可能重写,该方法避免了强制打开可选值并轻度处理潜在的错误:

func readJson() {
    // Get url for file
    guard let fileUrl = Bundle.main.url(forResource: "Data", withExtension: "json") else {
        print("File could not be located at the given url")
        return
    }

    do {
        // Get data from file
        let data = try Data(contentsOf: fileUrl)

        // Decode data to a Dictionary<String, Any> object
        guard let dictionary = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else {
            print("Could not cast JSON content as a Dictionary<String, Any>")
            return
        }

        // Print result
        print(dictionary)
    } catch {
        // Print error if something went wrong
        print("Error: \(error)")
    }
}