JSON无效的UTF-8中间字节

vir*_*l82 30 json jackson

当(Jackson,本例)JSON引擎尝试解析一些未以UTF-8编码的JSON时,会发生此错误.

如何告诉引擎它应该期望与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);
Run Code Online (Sandbox Code Playgroud)

错误日志:

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.jackson.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.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.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.jackson.JsonParseException: Invalid UTF-8 middle byte 0x20
at [Source: org.apache.http.conn.EofSensorInputStream@44d397b0; line: 92, column: 42]
at org.codehaus.jackson.JsonParser._constructError(JsonParser.java:1213)
at org.codehaus.jackson.impl.JsonParserMinimalBase._reportError(JsonParserMinimalBase.java:375)
at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidOther(Utf8StreamParser.java:2132)
at org.codehaus.jackson.impl.Utf8StreamParser._reportInvalidOther(Utf8StreamParser.java:2139)
at org.codehaus.jackson.impl.Utf8StreamParser._decodeUtf8_3fast(Utf8StreamParser.java:1962)
Run Code Online (Sandbox Code Playgroud)

McD*_*ell 29

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
Run Code Online (Sandbox Code Playgroud)

听起来服务器正在以某种非法编码(ISO-8859-1,windows-1252等)编码数据

  • 而FWIW,这正是杰克逊所做的 - 挑战只是ISO-8859-x在这个意义上确实看起来像UTF-8.但由于Unicode不允许使用非Unicode编码,因此有效JSON不存在问题. (2认同)
  • 就我而言,这是由于在创建文件时使用“content.toString().getBytes()”作为“FileObjectStream#write()”(其中“content”是“JsonNode”)引起的 - 它_看起来_相同,但是不是!修复方法是使用“new ObjectMapper().writeValueAsBytes(content)”。意识到这一点后,我将其添加到此处作为评论,以防其他人以与我相同的方式遇到此问题,这对他们有帮助 (2认同)

raz*_*ang 17

我在Java客户端应用程序中得到了这个异常,我正在序列化这样的JSON

String json = mapper.writeValueAsString(contentBean);
Run Code Online (Sandbox Code Playgroud)

在服务器端,我使用Spring Boot作为REST端点.例外是:

嵌套异常是com.fasterxml.jackson.databind.JsonMappingException:无效的UTF-8起始字节0xaa

我的问题是,我没有在HTTP客户端上设置正确的编码.这解决了我的问题:

updateRequest.setHeader("Content-Type", "application/json;charset=UTF-8");
StringEntity entity= new StringEntity(json, "UTF-8");
updateRequest.setEntity(entity);
Run Code Online (Sandbox Code Playgroud)

Android设置内容类型为HttpPost

  • 这对我有用,我没有写出字符串,而是使用了。MediaType.APPLICATION_JSON_UTF8_VALUE (2认同)

Epi*_*rce 6

我在使用 Notepad2 保存 JSON 文件后得到了这个,所以我必须使用 Notepad++ 打开它,然后说“转换为 UTF-8”。然后就成功了。