JSON 无效的 UTF-8 中间字节
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6352861/
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
JSON Invalid UTF-8 middle byte
提问by virtual82
This error happens when the (Hymanson, this case) JSON engine tries to parse some JSON that is not encoded in UTF-8.
当(Hymanson,本例)JSON 引擎尝试解析一些未以 UTF-8 编码的 JSON 时,会发生此错误。
How to tell the engine that it should expect something different from UTF-8, such as UTF-16?
如何告诉引擎它应该期望与 UTF-8 不同的东西,例如 UTF-16?
HttpHeaders requestHeaders = createSomeHeader();
RestTemplate restTemplate = new RestTemplate();
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
String url = "someurl"
ResponseEntity<MyObject[]> arrayResponseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, MyObject[].class);
error log:
错误日志:
Caused by: org.springframework.http.converter.HttpMessageNotReadableException: Could not read JSON: Invalid UTF-8 middle byte 0x20
at [Source: org.apache.http.conn.EofSensorInputStream@44d397b0; line: 92, column: 42]; nested exception is org.codehaus.Hymanson.JsonParseException: Invalid UTF-8 middle byte 0x20
at [Source: org.apache.http.conn.EofSensorInputStream@44d397b0; line: 92, column: 42]
at org.springframework.http.converter.json.MappingHymansonHttpMessageConverter.readInternal(MappingHymansonHttpMessageConverter.java:138)
at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:154)
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:74)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:622)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:608)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:449)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:404)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:380)
... 4 more
Caused by: org.codehaus.Hymanson.JsonParseException: Invalid UTF-8 middle byte 0x20
at [Source: org.apache.http.conn.EofSensorInputStream@44d397b0; line: 92, column: 42]
at org.codehaus.Hymanson.JsonParser._constructError(JsonParser.java:1213)
at org.codehaus.Hymanson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:375)
at org.codehaus.Hymanson.impl.Utf8StreamParser._reportInvalidOther(Utf8StreamParser.java:2132)
at org.codehaus.Hymanson.impl.Utf8StreamParser._reportInvalidOther(Utf8StreamParser.java:2139)
at org.codehaus.Hymanson.impl.Utf8StreamParser._decodeUtf8_3fast(Utf8StreamParser.java:1962)
采纳答案by McDowell
JSON data must be encoded as UTF-8, UTF-16 or UTF-32. The JSON decoder can determine the encoding by examining the first four octetsof the byte stream:
JSON 数据必须编码为 UTF-8、UTF-16 或 UTF-32。JSON 解码器可以通过检查字节流的前四个八位字节来确定编码:
00 00 00 xx UTF-32BE
00 xx 00 xx UTF-16BE
xx 00 00 00 UTF-32LE
xx 00 xx 00 UTF-16LE
xx xx xx xx UTF-8
It sounds like the server is encoding data in some illegal encoding (ISO-8859-1, windows-1252, etc.)
听起来服务器正在以某种非法编码(ISO-8859-1、windows-1252 等)对数据进行编码
回答by razvang
I got this exception when in the Java Client Application I was serializing a JSON like this
我在 Java 客户端应用程序中序列化这样的 JSON 时遇到此异常
String json = mapper.writeValueAsString(contentBean);
and on the Server Side I was using Spring Boot as REST Endpoint. Exception was:
在服务器端,我使用 Spring Boot 作为 REST 端点。例外是:
nested exception is com.fasterxml.Hymanson.databind.JsonMappingException: Invalid UTF-8 start byte 0xaa
嵌套异常是 com.fasterxml.Hymanson.databind.JsonMappingException: Invalid UTF-8 start byte 0xaa
My problem was, that I was not setting the correct encoding on the HTTP Client. This solved my problem:
我的问题是,我没有在 HTTP 客户端上设置正确的编码。这解决了我的问题:
updateRequest.setHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity entity= new StringEntity(json, "UTF-8");
updateRequest.setEntity(entity);
回答by EpicPandaForce
I got this after saving the JSON file using Notepad2, so I had to open it with Notepad++ and then say "Convert to UTF-8". Then it worked.
我在使用 Notepad2 保存 JSON 文件后得到了这个,所以我不得不用 Notepad++ 打开它,然后说“转换为 UTF-8”。然后它起作用了。
回答by ScottWelker
On the off chance it may help others I'll share a related anecdote.
如果它可能会帮助其他人,我将分享一个相关的轶事。
I encountered this exact error (Invalid UTF-8 middle byte 0x3f) running a PowerShell script via the PowerShell Integrated Script Environment (ISE). The identical script, executed outside the ISE, works fine. The code uses the Confluence v3 and v5.x REST APIs and this error is logged on the Confluence v5.x server - presumably because the ISE somehow mucks with the request.
我在通过 PowerShell集成脚本环境 (ISE)运行 PowerShell 脚本时遇到了这个确切的错误(无效的 UTF-8 中间字节 0x3f)。在 ISE 外部执行的相同脚本运行良好。该代码使用 Confluence v3 和 v5.x REST API,并且此错误记录在 Confluence v5.x 服务器上 - 大概是因为 ISE 以某种方式处理了请求。
回答by user3610939
I had this problem inconsistently between different platforms, as I got JSON as String from Mapper and did the writing myself. Sometimes it went into file as ansi and other times correctly as UTF8. I switched to
我在不同平台之间遇到了不一致的问题,因为我从 Mapper 获得了 JSON 作为 String 并自己编写。有时它以 ansi 的形式进入文件,有时以 UTF8 的形式正确地进入文件。我切换到
mapper.writeValue(file, data);
letting Mapper do the file operations, and it started working fine.
让 Mapper 进行文件操作,它开始工作正常。

