Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save EndLess728/2de57dbce8d440ecac591ca36e848db3 to your computer and use it in GitHub Desktop.
Save EndLess728/2de57dbce8d440ecac591ca36e848db3 to your computer and use it in GitHub Desktop.
Convert Dictionary to a JSON string in swift
extension Dictionary {
func jsonString() -> NSString? {
let jsonData = try? JSONSerialization.data(withJSONObject: self, options: [])
guard jsonData != nil else {return nil}
let jsonString = String(data: jsonData!, encoding: .utf8)
guard jsonString != nil else {return nil}
return jsonString! as NSString
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment