如何用空值替换 \"

How to replace \" with empty value

我有一个 JSON 字符串:

{
    "key1": "abc",
    "key2": "def",
    "key3": "gh\"i\"j"
}

预计o/p:

{
    "key1": "abc",
    "key2": "def",
    "key3": "ghij"
}

Java 字符串 replace()replaceAll() 正在替换所有双引号:

System.out.println(a.replaceAll("\\"",""));

System.out.println(a.replace("\"",""));

输出:

{
    key1: abc,
    key2: def,
    key3: ghij
}

我尝试替换 \" 的原因是必须使用 JSON 完成某些操作,转义特殊字符并将 JSON 字符串存储到数据库中。这里的 json 因为 \".

变得无效

如何将 \" 替换为空值?

您想将 \" 替换为空字符串。

\在正则表达式中有特殊含义,需要转义。因此,您需要将 \" 替换为空字符串。

然后,在java中的字符串中写入字符串\"需要转义每个\ + ".

因此,表达式为\ \ \"(为了便于阅读,我添加了一些空格):

最后,你需要这样写:

a.replaceAll("\\\"", "");