JSON Parsing in Swift 3

前端 未结 8 848
暖寄归人
暖寄归人 2020-12-01 11:05

Has anyone been able to find a way to parse through JSON files in Swift 3? I have been able to get the data to return but I am unsuccessful when it comes to breaking the dat

8条回答
  •  感动是毒
    2020-12-01 11:29

    Shoving JSON into a string manually is a pita. Why don't you just put the JSON into a file and read that in?

    Swift 3:

    let bundle = Bundle(for: type(of: self))
        if let theURL = bundle.url(forResource: "response", withExtension: "json") {
            do {
                let data = try Data(contentsOf: theURL)
                if let parsedData = try? JSONSerialization.jsonObject(with: data) as! [String:Any] {
                    grok(parsedData)
                }
            } catch {
                print(error)
            }
        }
    

提交回复
热议问题