将服务器返回的JSON string转化成字典时报错:
Error Domain=NSCocoaErrorDomain Code=3840 "Invalid escape sequence around character 586."
仔细查找后在原来解析的基础上将"\"字符替换成""或"\\"后,解析成功。具体的解析代码如下:
- (NSDictionary *)parseJsonStringToNSDictionary:(NSString *)jsonString { jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\r\n" withString:@""]; jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""]; jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\t" withString:@""]; jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\" withString:@""]; NSError *error2=nil; NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error2]; if ([dict isValid]) { return dict; } return nil; }
时间: 2024-10-13 08:33:38