IOS json Parsing Encountered Error Problem Solution

  • 2021-09-24 23:52:25
  • OfStack

Summary:

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.}

Previously, json was parsed in standard format, and there were no tabs such as\ n\ r\ t in json data.

Today, when analyzing, it is found that json analysis is good and bad, and online json analysis is also problematic. After searching for half a day, I finally found that tabs are at fault, because the standard json parsing is not allowed to have these tabs. Therefore, we need to filter out these tabs when we receive heat preservation.


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;



//The parsing mode of the system itself.


NSDictionary * userInfo = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableLeaves error:&e];

if (e) {

NSLog(@"%@",e);

}

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: