iOS - NSJSON 序列化 : Unable to convert data to string around character

标签 ios objective-c xcode cocoa-touch nsjsonserialization

解析 JSON 时出现此错误:

NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];

Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 3840.)" (Unable to convert data to string around character 73053.) UserInfo=0x1d5d8250 {NSDebugDescription=Unable to convert data to string around character 73053.}

有什么解决办法吗?

已添加 正如它在错误报告中所说,解析器无法通过位置 73053 处的字符,它在我的 JSON 响应中是“ø”。据我所知,Ø、Å、Æ 等字符对于 json 解析器应该不是问题吗?

最佳答案

是的,我在编码问题上遇到了同样的问题,并得到了上述错误。我从服务器获取 NSData 作为 encoding:NSISOLatin1StringEncoding。所以我必须在使用 NSJSONSerialization 解析它之前将它转换为 UTF8。

NSError *e = nil;
NSString *iso = [[NSString alloc] initWithData:d1 encoding:NSISOLatin1StringEncoding];
NSData *dutf8 = [iso dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:dutf8 options:NSJSONReadingMutableContainers error:&e];

关于iOS - NSJSON 序列化 : Unable to convert data to string around character,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14321033/

相关文章:

ios - 如何唯一标识选择器?

ios - iBeacon 错误 : Setting up major and minor id's

objective-c - Xcode中私有(private)方法的单元测试

iphone - 无法安装从 Xcode 4.5 创建的 .ipa

swift - Xcode segues 不工作

ios - Xcode:无法在 Apple Watch 模拟器 ("Will Install Watch App timed out"上安装应用程序)

ios - SpriteKit - 检测节点是否命中另一个子节点并执行操作

iphone - 在 Objective C 中解析二维数组

ios - 如何修复 UIDatePicker 中的最大年份而不是日期和月份?

ios - 包装在 UIViewController 中的 UICollectionView