iOS解析JSON字符串报错Error Domain=NSCocoaErrorDomain Code=3840 "Invalid escape sequence around character 586."

将服务器返回的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;
}
上一篇:如何获取ABBYY FineReader 12注册码-激活码-序列号


下一篇:把旧系统迁移到.Net Core 2.0 日记(2) - 依赖注入/日志NLog