Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Along with Swift 4 (Foundation) now it is natively supported in both ways, JSON string to an object - an object to JSON string. Please see Apple's documentation here JSONDecoder() and here JSONEncoder()
- JSON String to Object
- let jsonData = jsonString.data(using: .utf8)!
- let decoder = JSONDecoder()
- let myStruct = try! decoder.decode(myStruct.self, from: jsonData)
- Swift Object to JSONString
- let encoder = JSONEncoder()
- encoder.outputFormatting = .prettyPrinted
- let data = try! encoder.encode(myStruct)
- print(String(data: data, encoding: .utf8)!)
- You can find all details and examples here Ultimate Guide to JSON Parsing With Swift 4
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement