java jackson 中的意外字符('\'(代码 92))
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36646693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unexpected character ('\' (code 92)) in Hymanson
提问by basit raza
Am facing issue while parsing JSON string with Hymanson in some cases.
在某些情况下,使用 Hymanson 解析 JSON 字符串时遇到问题。
String jsonString = "{\"Age\":40, \"Name\":\"Sample User\"}";
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(jsonString,JsonNode.class);
System.out.println(jsonstirng)
{"Age":40, "Name":"Sample User"}
Above code works well when I pass jsonString
value.
当我传递jsonString
值时,上面的代码运行良好。
In some cases I need to escape invalid string characters like ",'
etc
在某些情况下,我需要转义无效的字符串字符",'
等
For escaping I am using ApacheStringEscapeUtils
.
为了逃避,我正在使用ApacheStringEscapeUtils
。
String escapedString = StringEscapeUtils.escapeJson(jsonStirng);
Escaped String output
转义字符串输出
{\"Age\":40,\"Name\":\"Sample User\"}
When I pass escapedString to mapper
its throw Unexpected character exception.
当我将 escapedString 传递给mapper
它的 throw 意外字符异常时。
ObjectMapper mapper = new ObjectMapper();
mapper.readValue(escapedString,JsonNode.class);
Exception
例外
Unexpected character ('\' (code 92)): was expecting double-quote to start field name
Actually Am parsing ModSecurity audit logs. Response body of audit log has (html,css,javascript etc) stuff that's why I need to escape JSON string other wise its breaks the JSON format.
实际上是解析 ModSecurity 审计日志。审核日志的响应正文具有(html、css、javascript 等)内容,这就是为什么我需要转义 JSON 字符串,否则它会破坏 JSON 格式。
回答by Peter Lawrey
The purpose of escaping a String is to make it unparsable as piece of JSON.
转义字符串的目的是使其无法解析为 JSON。
In your cause you are replacing all the "
with \"
so that it can be used inside a String value and the parser will NOT see it as part of the JSON.
在您的原因中,您将所有的替换为"
with,\"
以便它可以在字符串值中使用,并且解析器不会将其视为 JSON 的一部分。
e.g. this is just one field and value;
例如,这只是一个字段和值;
"myJson": "{\"Age\":40,\"Name\":\"Sample User\"}"
what you can't do is the following which is why you need this method. It can't tell the difference between "
which starts/ends the string and the "
inside the string.
您不能做的是以下这就是您需要此方法的原因。它无法分辨"
哪个开始/结束字符串和"
字符串内部之间的区别。
"myJson": "{"Age":40,"Name":"Sample User"}"
If you then try to parse this escaped String, it shouldn't be able to parse it.
如果您随后尝试解析此转义字符串,它应该无法解析它。
EDIT: Here is an example
编辑:这是一个例子
String text = "{\"Age\":40,\"Name\":\"Sample User\"}";
String escaped = StringEscapeUtils.escapeJson(text);
System.out.println("escaped= " + escaped);
String unescaped = StringEscapeUtils.unescapeJson(escaped);
System.out.println("unescaped= " + unescaped);
prints
印刷
escaped= {\"Age\":40,\"Name\":\"Sample User\"}
unescaped= {"Age":40,"Name":"Sample User"}
you can see that the escaped string has \"
however the unescaped string doesn't. If you are still seeing \
I would assume the String hasn't been unescaped.
你可以看到转义的字符串有\"
但未转义的字符串没有。如果您仍然看到\
我会假设 String 没有被转义。