I have a NSString that may contain quotes,\, /, \r, \n, and I want to convert it to a JSON encoded string so strings like this
"text1\text2"
becomes
\"text1\\text2\"
Is there a existing function to let me do this?
Also, I am using SBJson in my project but I cannot find whether SBJson can do this or not.
NSJSONSerialization is not on the table since my application still needs to support OSX 10.6
Does this answer your question?
Source: converting NSString to JSON string
Swift 2:
Swift 3:
Very useful to prevent error:
Domain=NSCocoaErrorDomain Code=3840 "Invalid escape sequence around character 123."
let's do it in more-native-JSON-way ;)
If u like my answer, please star me at https://github.com/wanjochan
Swift 4 Solution