Skip to content

Instantly share code, notes, and snippets.

@pawan-preet
Created March 13, 2018 05:36
Show Gist options
  • Save pawan-preet/8c7c2e7c7eb44cf5586a9d85700a7ecb to your computer and use it in GitHub Desktop.
Save pawan-preet/8c7c2e7c7eb44cf5586a9d85700a7ecb to your computer and use it in GitHub Desktop.
Get JSON String from a Dictionary
extension Dictionary {
var json: String {
let invalidJson = "Error: Invalid JSON"
do {
let jsonData = try JSONSerialization.data(withJSONObject: self, options: .prettyPrinted)
return String(bytes: jsonData, encoding: String.Encoding.utf8) ?? invalidJson
} catch {
return invalidJson
}
}
func getJSONString() -> String {
return json
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment