Skip to content

Instantly share code, notes, and snippets.

@BasThomas
Created December 1, 2016 15:00
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save BasThomas/0f894d610f7f24732077fa961a6fc182 to your computer and use it in GitHub Desktop.
import Foundation
let jsonString = "{\"message\": \"422 Unprocessable Entity\",\"errors\": {\"email\": [\"The email has already been taken.\"],\"mobile_number\" : [\"The mobile number has already been taken.\"]}}"
let jsonData = jsonString.data(using: .utf8)
do {
if let jsonData = jsonData {
let object = try JSONSerialization.jsonObject(with: jsonData, options: []) as? [AnyHashable: Any]
}
} catch {
print(error)
}
@BasThomas
Copy link
Author

This prints

Optional([AnyHashable("errors"): {
    email =     (
        "The email has already been taken."
    );
    "mobile_number" =     (
        "The mobile number has already been taken."
    );
}, AnyHashable("message"): 422 Unprocessable Entity])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment