JsonParseException:非法无引号字符((CTRL-CHAR,代码10)



我试图使用org.apache.httpcomponents来消费Rest API,它将向API发布JSON格式的数据。

我得到了这个异常:

由:com.fasterxml.jackson.core.JsonParseException: Illegal引起无引号字符((CTRL-CHAR,代码10)):必须使用在字符串中包含反斜杠

原因是ctrl-char包含在JSON字符串中。

有什么办法可以代替这个或其他的解决方案吗?

如果JSON字符串文本中有换行符(或其他控制字符),则可能发生这种情况。

{"foo": "bar
baz"}

如果你是生成数据的人,在创建字符串字面量时用转义的"\n"替换实际的换行符。

{"foo": "barnbaz"}

使用

mapper.configure(
    JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature(), 
    true
);

看到javadoc:

/**
 * Feature that determines whether parser will allow
 * JSON Strings to contain unescaped 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.
 *<p>
 * Since JSON specification requires quoting for all control characters,
 * this is a non-standard feature, and as such disabled by default.
 */

旧选项JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS自2.10起已弃用。

在Salesforce平台上,此错误是由/引起的,解决方案是将这些转义为//

当您向服务器发送JSON数据时发生此错误。也许在您的字符串中,您试图通过使用/n.

添加新的行字符。

如果你在/n之前加上/,它应该工作,你需要转义新行字符。

"Hello there //n start coding"

结果如下

Hello there
start coding

JsonParseException:非法无引号字符((CTRL-CHAR,代码10)

解决方案:在你的文本中存在一个chr TAB,在将文本放入json之前,使用替换chr TAB到t

最新更新