thieumao

Dict - Json

Jun 4th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. let jsonObject: [String: Any] = [
  2. "firstname": "aaa",
  3. "lastname": "sss",
  4. "email": "my_email",
  5. "nickname": "ddd",
  6. "password": "123",
  7. "username": "qqq"
  8. ]
  9.  
  10. do {
  11. let jsonData = try JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted)
  12. // here "jsonData" is the dictionary encoded in JSON data
  13.  
  14. let decoded = try JSONSerialization.jsonObject(with: jsonData, options: [])
  15. // here "decoded" is of type `Any`, decoded from JSON data
  16.  
  17. // you can now cast it with the right type
  18. if let dictFromJSON = decoded as? [String:String] {
  19. print(dictFromJSON)
  20. }
  21. } catch {
  22. print(error.localizedDescription)
  23. }
Advertisement
Add Comment
Please, Sign In to add comment