简体   繁体   中英

com.fasterxml.jackson.core.JsonParseException: Unrecognized character escape 'U' (code 85)

I've been searching for a solution for hours but without succes. I have a json string which I try to map to my java model, but running the code I get the above exception. JSON String:

{"productOptions" : {"2-bruch Kreuzfalz" : "nein"},"uploadData" : {"20682967" : {"mergedFile" : "C:\Users\userName\IdeaProjects\projectName\target\test-classes\com\flyeralarm\tools\kronos\order\integration\input\DE151886742X01_20150727131135.pdf"}}}

To save time and space I won't post the java model code but if it'll be needed to solve the task I'd do it. I suspect that the error comes because the path to the merged file contains 'C:\\Users' where a backslash is followed by the char 'U'. Since it is only a suspect I am not quite sure what to do. Am I doing sth. wrong? How can I solve the error?

UPDATE

If I am right and jackson tries to escape the 'U' in the path, then why is it so? I intentionally put the path into the quotes. Why does then jackson considers the slash as an escape attempt?

The problem is that the original JSON in your question is not valid.

According to the JSON specification , the (BNF) syntax for a string is:

string ::= "" 
           " chars "
chars  ::= char
           char chars
char   ::= any-Unicode-character-except-"-or-\-or-control-character
           \"
           \\
           \/
           \b
           \f
           \n
           \r
           \t
           \u four-hex-digits 

In English, that means that a backslash in a string is an escape character, and it CANNOT be followed by a capital U .


If I am right and jackson tries to escape the 'U' in the path, then why is it so?

Because the JSON specification says that it MUST do that. The problem is with your understanding of JSON, not in the behaviour of Jackson.

If you want to include literal backslashes in a JSON string, you need to escape them ... with backslashes; eg

    {"mergedFile" : "C:\\Users\\userName\\frobbit.pdf"}

Just tried to change the backslash into a normal slash and it worked. Kinda weird that I am forced to use only one type of slashes but at least it works now. To clarify: Changing

"C:\Users\userName\IdeaProjects\projectName\target\test-classes\com\flyeralarm\tools\kronos\order\integration\input\DE151886742X01_20150727131135.pdf"

into

"C:/Users/a.eirich/IdeaProjects/kronos/src/test/resources/com/flyeralarm/tools/kronos/order/integration/input/DE151886742X01_20150727131135.pdf"

did the job.

I know this is a bit late, but with 2 backlashes thrown the same error

With 4 backlashes \\\\\\\\ works

I have this record in the MySQL database "{\\"mergedFile\\" : \\"C:\\\\\\\\Users\\\\\\\\userName\\\\\\\\frobbit.pdf\\"}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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