简体   繁体   English

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

[英]How to parse json string with UTF-8 characters using java?

I have a json string with SUBSTITUTE () utf-8 character. 我有一个带有SUBSTITUTE ()utf-8字符的json字符串。 I'm getting parsing exception when I try to convert json string to java object using jackson. 当我尝试使用jackson将json字符串转换为java对象时,我正在解析异常。 Can you please let me know how to encode and decode utf-8 characters ? 你能告诉我如何编码和解码utf-8字符吗?

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

jsonString: jsonString:

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

在此输入图像描述

Error: 错误:

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

Can you try this? 你能试试吗?

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

I hope it helps you: Javadoc 我希望它可以帮到你: 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. 确定解析器是否允许JSON字符串包含不带引号的控制字符(值小于32的ASCII字符,包括制表符和换行符)的功能。 If feature is set false, an exception is thrown if such a character is encountered. 如果feature设置为false,则在遇到此类字符时会引发异常。 Since JSON specification requires quoting for all control characters, this is a non-standard feature, and as such disabled by default. 由于JSON规范要求引用所有控制字符,因此这是非标准功能,因此默认情况下禁用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM