Skip to content

Instantly share code, notes, and snippets.

@BasThomas
Created December 1, 2016 15:00
Show Gist options
  • Save BasThomas/0f894d610f7f24732077fa961a6fc182 to your computer and use it in GitHub Desktop.
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