示例代码:
NSString *jsonObject = ...;
BOOL isValidJSONObject = [NSJSONSerialization isValidJSONObject:jsonObject];
id jsonResponse = [NSJSONSerialization JSONObjectWithData:parsedData
options:NSJSONReadingAllowFragments
error:&jsonError];
问题:
如果jsonObject
包含带换行符的字符串数据,则NSJSONSerialization
无法解析并返回有效的jsonRespone
对象。它返回以下错误:
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.}
示例JSON字符串(带换行符):
{"Name" : "Lorum","Description" : "Sample:
Lorum ipsum","Quantity" : 1,"Type" : 1}
处理这种情况的最佳方法是什么?在这种情况下,换行符/换行符应该有效。