gpt4 book ai didi

java - 如何用空值替换\"

转载 作者:行者123 更新时间:2023-11-30 10:28:51 28 4
gpt4 key购买 nike

我有一个 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("\\\\\"", "");

关于java - 如何用空值替换\",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44366257/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com