gpt4 book ai didi

java - 如何使用java解析带有UTF-8字符的json字符串?

转载 作者:行者123 更新时间:2023-11-29 04:10:05 26 4
gpt4 key购买 nike

我有一个带有 SUBSTITUTE 的 json 字符串() utf-8 字符。当我尝试使用 jackson 将 json 字符串转换为 java 对象时出现解析异常。你能告诉我如何编码和解码 utf-8 字符吗?

ObjectMapper mapper = new ObjectMapper();
mapper.readValue(jsonString, MY_DOMAIN_OBJECT.class);

json字符串:

{"studentId":"753253-2274", "information":[{"key":"1","value":"Get alerts on your phone(SUBSTITUTE character is present here. Unable to paste it)To subscribe"}]}

enter image description here

错误:

Illegal unquoted character ((CTRL-CHAR, code 26)): has to be escaped using backslash to be included in string value

最佳答案

你能试试这个吗?

ObjectMapper mapper = new ObjectMapper();
mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
mapper.readValue(jsonString, MY_DOMAIN_OBJECT.class);

希望对您有所帮助: Javadoc

Feature that determines whether parser will allow JSON Strings to contain unquoted control characters (ASCII characters with value less than 32, including tab and line feed characters) or not. If feature is set false, an exception is thrown if such a character is encountered. Since JSON specification requires quoting for all control characters, this is a non-standard feature, and as such disabled by default.

关于java - 如何使用java解析带有UTF-8字符的json字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55576620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com