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!
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"}
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.
*/
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!
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