简体   繁体   English

如何在Java中的JSON解析器的字符串中转义双引号

[英]How to escape double quotes in a string for json parser in Java

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. 上面的字符串只是一个示例,请注意,并非字符串中的所有双引号都应转义,仅应转义例如“ charlie”和““”“中的双引号,否则json解析器将无法正确解析该字符串。 Expected result should be like: 预期结果应为:

{
    "test": [
        "",
        "abc",
        "IF(Var218 = \"charlie\") AND (Var85 ≤ 0) AND (Var207 = \"some value\"; \"du\") THEN Appetency = 1 ",
        "",
        "\"\""
    ]
}

Thanks. 谢谢。

I'm using the Gson library for this. 我正在为此使用Gson库。 But it looks like this is what you are asking for. 但看起来这就是您要的。

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 &le; 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 &le; 0) AND (Var207 = \"some value\"; \"du\") THEN Appetency = 1 ",
    "",
    "\"\""
  ]
}

In java you escape " by \\" and the same goes for JSON. 在Java中,您使用" \\"转义,JSON也是如此。 The problem is that \\" does not include a backward slash, so you need to escape that as well: 问题在于\\"不包含反斜杠,因此您也需要转义该斜杠:

String json = "hello \\\"world\\\""; // hello "world"

Notice the three (3) backwards slash. 注意三(3)个反斜杠。

I don;t see the point bacause you are escaping your cdouble quoted correct 我看不到这一点,因为您正在转义正确的双引号

"\"\"" // this will produce ""

So simple before each " place one \\ 如此简单在每个“放置一个”之前

The following algorithm should work with the JSON String in input 以下算法应在input使用JSON字符串

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM