java 如何在Java中为json解析器转义字符串中的双引号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26501523/
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
How to escape double quotes in a string for json parser in Java
提问by Bruce Li
I got a string like below:
我得到一个像下面这样的字符串:
{
"test": [
"",
"abc",
"IF(Var218 = "charlie") AND (Var85 ≤ 0) AND (Var207 = \"some value\"; \"du\") THEN Appetency = 1 ",
"",
""""
]
}
The string will be parsed as a JSON object, now the question is how should I escape some of the double quotes in the string in Java effectively?
该字符串将被解析为 JSON 对象,现在的问题是我应该如何有效地转义 Java 中字符串中的一些双引号?
The string above is just one example, and please notice not all double quotes in the string should be escaped, only double quotes such as in "charlie" and """" should be escaped, otherwise json parser will not parse the string correctly. Expected result should be like:
上面的字符串只是一个例子,请注意不是字符串中的所有双引号都应该转义,只有"charlie"和""""这样的双引号应该转义,否则json解析器将无法正确解析字符串。预期结果应该是这样的:
{
"test": [
"",
"abc",
"IF(Var218 = \"charlie\") AND (Var85 ≤ 0) AND (Var207 = \"some value\"; \"du\") THEN Appetency = 1 ",
"",
"\"\""
]
}
Thanks.
谢谢。
回答by Nick Allen
I'm using the Gson library for this. But it looks like this is what you are asking for.
为此,我正在使用 Gson 库。但看起来这就是你所要求的。
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
...
public void stuff()
{
List<String> data = new ArrayList<String>();
data.add("");
data.add("abc");
data.add("IF(Var218 = \"charlie\") AND (Var85 ≤ 0) AND (Var207 = \"some value\"; \"du\") THEN Appetency = 1 ");
data.add("\"\"");
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
JsonObject test = new JsonObject();
JsonElement jsonData = gson.toJsonTree(data, new TypeToken<List<String>>(){}.getType());
test.add("test", jsonData);
String json = gson.toJson(test);
System.out.println(json);
}
This will produce:
这将产生:
{
"test":[
"",
"abc",
"IF(Var218 = \"charlie\") AND (Var85 ≤ 0) AND (Var207 = \"some value\"; \"du\") THEN Appetency = 1 ",
"",
"\"\""
]
}
回答by SamTebbs33
The following algorithm should work with the JSON String in input
以下算法应与 JSON 字符串一起使用 input
String progress = "";
for(int c = 0; c < input.length(); c++){
char ch = input.charAt(c);
if(ch == '\'){ // Skip if the next character is already escaped
c++;
continue;
}
if(ch == '\"') progress += "\\""; // Results in \"
else progress += ch; // Add the character to progress
}
The escaped String is now in progress
转义的字符串现在在 progress
回答by Baked Inhalf
In java you escape "
by \"
and the same goes for JSON. The problem is that \"
does not include a backward slash, so you need to escape that as well:
在Java中你逃脱"
被\"
和同样的JSON。问题是\"
不包括反斜杠,所以你也需要转义:
String json = "hello \\"world\\""; // hello "world"
Notice the three (3) backwards slash.
请注意三 (3) 个反斜杠。
回答by Beri
I don;t see the point bacause you are escaping your cdouble quoted correct
我不明白这一点,因为你在逃避你的 cdouble 引用是正确的
"\"\"" // this will produce ""
So simple before each " place one \
如此简单,在每个"之前放一个\