Jackson没有在JSON中转义引号

时间:2021-10-06 22:29:53

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.)

我在java中尝试将json放入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 jackson, I get the first, good result:

但是当我把它直接写进jackson的文件时,我得到了第一个,很好的结果:

new ObjectMapper().writer().writeValue(fileToWriteTo, booksMessage);

So why does jackson escape differently when writing to a file, and how do I get it to escape like that for me when writing to a string?

那么,为什么当写入文件时,jackson会以不同的方式逃离?我如何让它在写入一个字符串时像这样逃离呢?

2 个解决方案

#1


5  

The writeValue() methods of the ObjectWriter class encode the input text.

ObjectWriter类的writeValue()方法对输入文本进行编码。

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();

#2


2  

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转义的情况下对文件进行写操作。

#1


5  

The writeValue() methods of the ObjectWriter class encode the input text.

ObjectWriter类的writeValue()方法对输入文本进行编码。

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();

#2


2  

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转义的情况下对文件进行写操作。