Advertisement
Guest User

Untitled

a guest
Jun 19th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. 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()
  2.  
  3. JSON String to Object
  4.  
  5. let jsonData = jsonString.data(using: .utf8)!
  6. let decoder = JSONDecoder()
  7. let myStruct = try! decoder.decode(myStruct.self, from: jsonData)
  8. Swift Object to JSONString
  9.  
  10. let encoder = JSONEncoder()
  11. encoder.outputFormatting = .prettyPrinted
  12. let data = try! encoder.encode(myStruct)
  13. print(String(data: data, encoding: .utf8)!)
  14. 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