【发布时间】:2017-11-06 01:38:39
【问题描述】:
我有一个 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因为\"而失效。
如何仅将 \" 替换为空值?
【问题讨论】:
-
replaceAll("\\\\\"", "")有效。但我仍在试图找出原因。看来你“逃得还不够”。 -
\\\\字符串转义后是\\,这是\的正则表达式转义
标签: java json replace replaceall