xcode iOS - NSJSONSerialization:无法将数据转换为字符周围的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14321033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:31:21  来源:igfitidea点击:

iOS - NSJSONSerialization: Unable to convert data to string around character

iosobjective-cxcodecocoa-touchnsjsonserialization

提问by Oleg

I'm getting this error while parsing JSON:

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

Any suggestions how to fix this?

任何建议如何解决这个问题?

ADDEDAs it says in error report, the parser can't go through the character at position 73053, which is "?" in my JSON response. As far as I know characters like ?,?,? etc. shouldn't be a problem for json parsers?

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

回答by karim

Yes, I have the same problem with encoding issue and got the above error. I got the NSData from server as encoding:NSISOLatin1StringEncoding. So I had to convert it to UTF8 before parsing it using NSJSONSerialization.

是的,我在编码问题上遇到了同样的问题,并得到了上述错误。我从服务器获得了 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];

回答by Chandra

Switf 3

3

let responseStrInISOLatin = String(data: data, encoding: String.Encoding.isoLatin1)
guard let modifiedDataInUTF8Format = responseStrInISOLatin?.data(using: String.Encoding.utf8) else {
      print("could not convert data to UTF-8 format")
      return
 }
do {
    let responseJSONDict = try JSONSerialization.jsonObject(with: modifiedDataInUTF8Format)
} catch {
    print(error)
}

回答by occulus

Check that the data you're parsing is actually valid JSON (and not just 'nearly' JSON). That error is known to occur when you have a different data format that can't be parsed as JSON. See for example:

检查您解析的数据是否实际上是有效的 JSON(而不仅仅是“接近”JSON)。当您拥有无法解析为 JSON 的不同数据格式时,就会发生该错误。见例如:

iOS 5 JSON Parsing Results in Cocoa Error 3840

iOS 5 JSON 解析结果 Cocoa 错误 3840

Do you have a top-level container in your JSON too? An array or dictionary. Example:

您的 JSON 中是否也有顶级容器?数组或字典。例子:

{ "response" : "Success" }

Update

更新

JSON's default encoding is UTF-8. Special/exotic characters aren't a problem for UTF-8, but please ensure that your server is returning its content properly encoded as UTF-8. Also, have you done anything to tell your JSON interpretter to use a different encoding?

JSON 的默认编码是 UTF-8。对于 UTF-8 而言,特殊/奇异字符不是问题,但请确保您的服务器返回其内容正确编码为 UTF-8。另外,你有没有告诉你的 JSON 解释器使用不同的编码?

If your JSON is coming from a web service, put the URL into this page to see what it has to see about the encoding:

如果您的 JSON 来自 Web 服务,请将 URL 放入此页面以查看有关编码的内容:

http://validator.w3.org/

http://validator.w3.org/

回答by Vishal

Swift 5:

斯威夫特 5:

Yes, i got the same error while parsing JSON data.

是的,我在解析 JSON 数据时遇到了同样的错误。

Solution : You have to first convert response data into String and then convert that Sting to Data using UTF8 encoding before decoding.

解决方案:您必须首先将响应数据转换为字符串,然后在解码之前使用 UTF8 编码将该字符串转换为数据。

let utf8Data = String(decoding: responseData, as: UTF8.self).data(using: .utf8)