在 Java 中转义 JSON 字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21576475/
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
Escape JSON string in Java
提问by Tombart
I'm using Google's com.google.api.client.json.GenericJson
and com.fasterxml.Hymanson.core.JsonGenerator
. I would like to serialize JSON object and escape quotes and backslashes so that I can pass that string in Bash. And afterwards deserialize that string.
我正在使用 Googlecom.google.api.client.json.GenericJson
和com.fasterxml.Hymanson.core.JsonGenerator
. 我想序列化 JSON 对象并转义引号和反斜杠,以便我可以在 Bash 中传递该字符串。然后反序列化该字符串。
GenericJson.toString
produces simple JSON, but \n
etc. are not escaped:
GenericJson.toString
产生简单的 JSON,但\n
等不会被转义:
{commands=ls -laF\ndu -h, id=0, timeout=0}
is there a simple way how to get something like this:
有没有一种简单的方法来获得这样的东西:
"{commands=\"ls -laF\ndu -h\", id=0, timeout=0}"
I don't want to reinvent the wheel, so I'd like to use Hymanson or an existing API, if possible.
我不想重新发明轮子,所以如果可能的话,我想使用 Hymanson 或现有的 API。
采纳答案by Barett
No additional dependencies needed: You're looking for JsonStringEncoder#quoteAsString(String)
.
不需要额外的依赖项:您正在寻找JsonStringEncoder#quoteAsString(String)
.
Click for JsonStringEncoder javadoc
Example:
例子:
import com.fasterxml.Hymanson.core.io.JsonStringEncoder;
JsonStringEncoder e = JsonStringEncoder.getInstance();
String commands = "ls -laF\ndu -h";
String encCommands = new String(e.quoteAsString(commands));
String o = "{commands: \"" + encCommands + "\", id: 0, timeout: 0}"
参考:http: //fasterxml.github.io/Hymanson-core/javadoc/2.1.0/com/fasterxml/Hymanson/core/io/JsonStringEncoder.html
回答by Tombart
Using Gson
for serialization proved to be quite easy and bulletproof. Afterwards Apache's commons-lang3 = 3.1
escapeEcmaScript
is used. In 3.2
there's also escapeJson
method.
使用Gson
序列化被证明是很容易的,防弹。之后使用Apache的commons-lang3 = 3.1
escapeEcmaScript
。在3.2
这里还有escapeJson
方法。
import com.google.api.client.json.GenericJson;
import com.google.api.client.util.Key;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringEscapeUtils;
public class MyJson extends GenericJson {
@Key("commands")
public String commands;
public String serialize() throws IOException {
Gson gson = new Gson();
String g = gson.toJson(this);
return StringEscapeUtils.escapeEcmaScript(g);
}
}
This produces escaped JSON:
这会产生转义的 JSON:
{\"commands\":\"ls -laF\ndu -h\"}
Deserialization is then quite simple:
反序列化非常简单:
protected MyJson deserialize(String str) throws IOException {
String json = StringEscapeUtils.unescapeEcmaScript(str);
JsonObjectParser parser = (new HymansonFactory()).createJsonObjectParser();
return parser.parseAndClose(new StringReader(json), MyJson.class);
}
The escapeEcmaScript
method isn't complicated, it does following replacement:
该escapeEcmaScript
方法并不复杂,它做了以下替换:
{"'", "\'"},
{"\"", "\\""},
{"\", "\\"},
{"/", "\/"}
But at least is something I don't have to care about.
但至少是我不必关心的事情。