java org.codehaus.jackson.JsonParseException: 意外字符 ('?' (code 65533 / 0xfffd))

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30660655/
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-11-02 17:27:41  来源:igfitidea点击:

org.codehaus.Hymanson.JsonParseException: Unexpected character ('?' (code 65533 / 0xfffd))

javajsonHymansonmapper

提问by Nikhil soni

I have a Json string in the database but while converting in Java object, it gives following error:

我在数据库中有一个 Json 字符串,但是在 Java 对象中转换时,它出现以下错误:

Caused by: org.codehaus.Hymanson.JsonParseException: Unexpected character ('?' (code 65533 / 0xfffd)): expected a valid value (number, String, array, object, 'true', 'false' or 'null')

引起:org.codehaus.Hymanson.JsonParseException:意外字符('?'(代码 65533 / 0xfffd)):预期有效值(数字、字符串、数组、对象、'true'、'false' 或 'null')

Json is : {"crt":"wrd","name":"7|6A TTTM"}

杰森是: {"crt":"wrd","name":"7|6A TTTM"}

In java code I have configured it and have made it private (not static final)

在 Java 代码中,我已经配置了它并将其设为私有(不是静态最终的)

objectMapper= new ObjectMapper();
objectMapper.configure(DeserializationConfig.Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);

Note:It some time converts that Json string in Object but some time gives above error. Why this unexpected result comes?

注意:有时会在 Object 中转换该 Json 字符串,但有时会出现上述错误。为什么会出现这个意想不到的结果?

回答by Mihnea

Short answer solution: Remove the first occurrence of the extra added BOM text with a method, such as the following, should fix this issue:

简答解决方案:使用如下方法删除第一次出现的额外添加的 BOM 文本,应该可以解决此问题:

public String cleanUpJsonBOM(String json) {
      return json.trim().replaceFirst("\ufeff", "");
  }

I had a similar issue which I documented in a blog post. Hope this help!

我在博客文章中记录了类似的问题。希望这有帮助!

回答by Priyamal

this worked for me.

这对我有用。

String formattedString = yourString.trim().replaceAll("\uFFFD", "");

回答by StaxMan

Something is producing invalid UTF-8 sequence (or, mismatch of UTF-8 vs a single-byte encoding like ISO-8859-1), and Hymanson detects this encoding problem. It has nothing to do with ACCEPT_SINGLE_VALUE_AS_ARRAYsetting, as the exception comes from low-level JsonParser.

某些东西正在产生无效的 UTF-8 序列(或者,UTF-8 与单字节编码(如 ISO-8859-1)不匹配),而 Hymanson 检测到此编码问题。它与ACCEPT_SINGLE_VALUE_AS_ARRAY设置无关,因为异常来自低级JsonParser.

So you need to figure out why the JSON content to parse is corrupt.

所以你需要弄清楚为什么要解析的 JSON 内容是损坏的。