escaping - Objective-C - How to convert NSString to escaped JSON string? -
i have nsstring may contain quotes,\, /, \r, \n, , want convert json encoded string strings
"text1\text2"
becomes
\"text1\\text2\"
is there existing function let me this?
also, using sbjson in project cannot find whether sbjson can or not.
nsjsonserialization not on table since application still needs support osx 10.6
does answer question?
-(nsstring *)jsonstring:(nsstring *)astring { nsmutablestring *s = [nsmutablestring stringwithstring:astring]; [s replaceoccurrencesofstring:@"\"" withstring:@"\\\"" options:nscaseinsensitivesearch range:nsmakerange(0, [s length])]; [s replaceoccurrencesofstring:@"/" withstring:@"\\/" options:nscaseinsensitivesearch range:nsmakerange(0, [s length])]; [s replaceoccurrencesofstring:@"\n" withstring:@"\\n" options:nscaseinsensitivesearch range:nsmakerange(0, [s length])]; [s replaceoccurrencesofstring:@"\b" withstring:@"\\b" options:nscaseinsensitivesearch range:nsmakerange(0, [s length])]; [s replaceoccurrencesofstring:@"\f" withstring:@"\\f" options:nscaseinsensitivesearch range:nsmakerange(0, [s length])]; [s replaceoccurrencesofstring:@"\r" withstring:@"\\r" options:nscaseinsensitivesearch range:nsmakerange(0, [s length])]; [s replaceoccurrencesofstring:@"\t" withstring:@"\\t" options:nscaseinsensitivesearch range:nsmakerange(0, [s length])]; return [nsstring stringwithstring:s]; }
source: converting nsstring json string
Comments
Post a Comment