Created
March 13, 2018 05:36
-
-
Save pawan-preet/8c7c2e7c7eb44cf5586a9d85700a7ecb to your computer and use it in GitHub Desktop.
Get JSON String from a Dictionary
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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