iOS json 解析遇到error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed.
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unescaped control character around character 1419.) UserInfo=0x1563cdd0 {NSDebugDescription=Unescaped control character around character 1419.}
之前解析json的时候都是标准格式,json数据当中没有 \n \r \t 等制表符。
今天在解析的时候发现json解析时好时坏,用在线json解析也米有问题。找了半天终于发现是制表符在作怪,由于标准的json解析是不允许有这几个制表符的。所以在收到保温的时候我们需要把这几个制表符给过滤掉。
NSString * responseString = [request responseString];
responseString = [responseString stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];
responseString = [responseString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
responseString = [responseString stringByReplacingOccurrencesOfString:@"\t" withString:@""];
NSLog(@"responseString = %@",responseString);
SBJsonParser *parser = [[[SBJsonParser alloc]init] autorelease];
id returnObject = [parser objectWithString:responseString];
NSDictionary *userInfo = nil;
NSArray *userArr = nil;
if ([returnObject isKindOfClass:[NSDictionary class]]) {
if (userInfo) {
[userArr release];
}
userInfo = (NSDictionary*)returnObject;
}
else if ([returnObject isKindOfClass:[NSArray class]]) {
userArr = (NSArray*)returnObject;
}
NSError* e = nil;
//系统自带的解析方式。
NSDictionary * userInfo = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:&e];
if (e) {
NSLog(@"%@",e);
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。