Checking if a string is valid JSON
Checking if a string is valid JSON
- Subject: Checking if a string is valid JSON
- From: "Eric E. Dolecki" <email@hidden>
- Date: Mon, 29 Jan 2018 20:17:09 +0000
I am generating a String of JSON. Before I use it, I want to check to make
sure that it's valid. My code is below. Does this look alright?
Thanks,
Eric
let jsonString = composedString
let jsonData = jsonString.data(using: String.Encoding.utf8)
do {
if (try JSONSerialization.jsonObject(with: jsonData!, options:
[]) as? NSDictionary) != nil {
print("JSON is a dictionary. Valid.")
} else {
print("Not valid JSON data.")
}
} catch let error as NSError {
print("Not valid JSON. \(error.localizedDescription)")
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden