Sample Code:
NSString *jsonObject = ...;
BOOL isValidJSONObject = [NSJSONSerialization isValidJSONObject:jsonObject];
id jsonResponse = [NSJSONSerialization JSONObjectWithData:parsedData
options:NSJSONReadingAllowFragments
error:&jsonError];
Issue:
If jsonObject
contains string data with newline characters, then NSJSONSerialization
fails to parse and return a valid jsonRespone
object. It returns following error:
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unescaped control character around character 119.) UserInfo=0x10ba9fef0 {NSDebugDescription=Unescaped control character around character 119.}
Sample JSON String (with newline character):
{"Name" : "Lorum","Description" : "Sample:
Lorum ipsum","Quantity" : 1,"Type" : 1}
What's the best way to handle this situation? Newline/ line break character should be valid in this case.
tag, and then replaced
with newline (at application end). Alternately, I could have encoded the data. I don't like this approach. I think NSJSONSerialization should handle the newline characters (\n and \r). JSONKit handles this with the option JKParseOptionUnicodeNewlines, and I like that! – Mustafa Aug 09 '14 at 12:55