Jackson报错Illegal unquoted character ((CTRL-CHAR, code 11)): has to be escaped using backslash to be included in string value
当你在使用 Jackson 库的时候,可能会遇到类似于 "Illegal unquoted character ((CTRL-CHAR, code 11))" 的报错信息。这个错误通常是由于字符串中包含了未转义的控制字符所引起的。
(1).原始代码
Java
代码语言:javascript复制// 转换为实体
ObjectMapper objectMapper = new ObjectMapper();
CorpChatMessage corpChatMessage = objectMapper.readValue(corpChatMessageJson, CorpChatMessage.class)
原始Json如下:
JSON
代码语言:javascript复制{"msgid":"11929905846625519600_11703315634393","action":"send","from":"ghldf","tolist":["wang1wf1"],"roomid":"","msgtime":1703315630081,"msgtype":"text","text":{"content":"直营亮点n 首月突破1080万"}}
很明显Json字符串包含了未转义的换行符
(2).解决代码
Java
代码语言:javascript复制objectMapper.configure(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(),true)
其中,JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS 表示允许未转义的控制字符,在调用 configure() 方法时将其设置为启用即可。