Simple and clean way to convert JSON string to Object in Swift

前端 未结 16 1179
挽巷
挽巷 2020-11-28 23:27

I have been searching for days to convert a fairly simple JSON string to an object type in Swift but with no avail.

Here is the code for web service call:



        
16条回答
  •  醉梦人生
    2020-11-29 00:13

    Using SwiftyJSON library, you could make it like

    if let path : String = Bundle.main.path(forResource: "tiles", ofType: "json") {
        if let data = NSData(contentsOfFile: path) {
            let optData = try? JSON(data: data as Data)
            guard let json = optData else {
                return
            }
            for (_, object) in json {
                let name = object["name"].stringValue
                print(name)
            }
        }
    } 
    

提交回复
热议问题