使用NSJSONSerialization解析json文件时,返回以error信息
Error Domain=NSCocoaErrorDomain Code=3840 "Invalid escape sequence around character 9511." UserInfo={NSDebugDescription=Invalid escape sequence around character 9511.}
解决方法:删除json字符串中的"",或者替换成"\"后,在使用NSJSONSerialization解析成功;
NSString *jsonFilePath = [[NSBundle mainBundle] pathForResource:@"Product.json" ofType:nil];
NSData *data = [NSData dataWithContentsOfFile:jsonFilePath];
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// 此处处理json字符串
jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\\" withString:@""];
NSError *error = nil;
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[jsonString dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
网友评论