NSJSONSerialization JSONObjectWithData其中数据包含换行符

时间:2014-08-09 07:10:16

标签: ios json nsjsonserialization

示例代码:

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}

处理这种情况的最佳方法是什么?在这种情况下,换行符/换行符应该有效。

0 个答案:

没有答案