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 protected])
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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]