JsonparseException Illegal unquoted character ((CTRL-CHAR, code 10)

╄→尐↘猪︶ㄣ 提交于 2019-12-17 15:43:32

问题


I'm trying to use org.apache.httpcomponents to consume a rest api ,which will post json format data to api.

while I get exception

Caused by: com.fasterxml.jackson.core.JsonParseException: Illegal unquoted character ((CTRL-CHAR, code 10)): has to be escaped using backslash to be included in string.

The reason is ctrl-char is included in the json string.

Is there any way to replace this .or some solution else?

thanks!


回答1:


This can happen if you have a newline (or other control character) in a JSON string literal.

{"foo": "bar
baz"}

If you are the one producing the data, replace actual newlines with escaped ones "\\n" when creating your string literals.

{"foo": "bar\nbaz"}



回答2:


Using

mapper.configure(Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);

See 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.
   *<p>
   * Since JSON specification requires quoting for all control characters,
   * this is a non-standard feature, and as such disabled by default.
   */



回答3:


I'd recommend you to use a text editor such as Vim to find if there are any (invisible) special or escape characters causing this issue.

Or if you are using windows, it's even simple... just copy-paste the code in windows notepad, and it will most likely show any invisible unwanted escape characters or line breaks etc. fix them and you're done!




回答4:


On Salesforce platform this error is caused by /, the solution is to escape these as //.



来源:https://stackoverflow.com/questions/31537153/jsonparseexception-illegal-unquoted-character-ctrl-char-code-10

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!