iOS json 解析遇到error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be comple
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
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);
}