Java 杰克逊没有在 JSON 中转义引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19870885/
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
Hymanson not escaping quotes in JSON
提问by CorayThan
I'm trying to put a json in a javascript file in java, but when I write the json to a string, the string doesn't appear to be a valid json for javascript; it is missing some escapes. (This is happening in a string in the json which I formatted as a faux json.)
我正在尝试将 json 放入 java 中的 javascript 文件中,但是当我将 json 写入字符串时,该字符串似乎不是 javascript 的有效 json;它缺少一些转义。(这是在 json 中的一个字符串中发生的,我将其格式化为人造 json。)
For example, this would be a valid json in my javascript file:
例如,这将是我的 javascript 文件中的有效 json:
{
"message":
"the following books failed: [{\"book\": \"The Horse and his Boy\",\"author\": \"C.S. Lewis\"}, {\"book\": \"The Left Hand of Darkness\",\"author\": \"Ursula K. le Guin\"}, ]"
}
Here's what I get, though, where the double quotes aren't escaped:
不过,这就是我得到的,双引号没有被转义:
{
"message":
"The following books failed: [{"book": "The Horse and his Boy","author": "C.S. Lewis"}, {"book": "The Left Hand of Darkness","author": "Ursula K. le Guin"}, ]"
}
I get the second result when I do this:
当我这样做时,我得到了第二个结果:
new ObjectMapper().writer().writeValueAsString(booksMessage);
But when I write it directly to a file with Hymanson, I get the first, good result:
但是当我用Hymanson直接把它写到一个文件中时,我得到了第一个很好的结果:
new ObjectMapper().writer().writeValue(fileToWriteTo, booksMessage);
So why does Hymanson escape differently when writing to a file, and how do I get it to escape like that for me when writing to a string?
那么为什么 Hymanson 在写入文件时会以不同的方式转义,以及在写入字符串时如何让它像我一样转义?
采纳答案by CorayThan
I added
我加了
booksJson = Pattern.compile("\\").matcher(booksJson).replaceAll("\\\\");
which escapes all the escape characters. That way when I write it to file and it removes the escapes, I still have the escapes I need. So turns out my real question was how to write to file without Java escapes being removed.
它转义了所有转义字符。这样当我将它写入文件并删除转义时,我仍然有我需要的转义。原来我真正的问题是如何在不删除 Java 转义的情况下写入文件。
回答by PNS
The writeValue()
methods of the ObjectWriter
class encode the input text.
类的writeValue()
方法ObjectWriter
对输入文本进行编码。
You don't need to write to a file. An alternative approach for getting the same string could be:
您不需要写入文件。获取相同字符串的另一种方法可能是:
StringWriter sw = new StringWriter();
new ObjectMapper().writer().writeValue(sw, booksMessage);
String result = sw.toString();
回答by dnuttle
I'm very late to the party but I faced a similar problem and I realized it was not a problem with Hymanson or my data. It was Java. I was reading from a JSON file and then trying to write it into a template HTML file.
我参加聚会很晚,但我遇到了类似的问题,我意识到这不是 Hymanson 或我的数据的问题。是爪哇。我正在读取 JSON 文件,然后尝试将其写入模板 HTML 文件。
I had a line my original JSON like yours, something like:
我有一条和你一样的原始 JSON 行,比如:
{"field" : "This field contains what looks like another JSON field: {\"abc\": \"value\"}"}
And when I wrote the above to a string, the backslash before the quotes in abc and value disappeared. I noticed that the contextual help for String.replaceAll mentioned something about Matcher.quoteReplacement. I went from this:
当我将上述内容写入字符串时,abc 和 value 中引号之前的反斜杠消失了。我注意到 String.replaceAll 的上下文帮助提到了一些关于 Matcher.quoteReplacement 的内容。我从这个出发:
template = template.replaceAll("%template%", jsonDataString);
to this:
对此:
Pattern pattern = Pattern.compile("%template%");
Matcher matcher = Pattern.matcher(template);
matcher.replaceAll(matcher.quoteReplacement(jsonDataString));
Problem solved.
问题解决了。