我需要将a\nb
替换为a\\nb
,使下面的字符串成为有效的json
众所周知,在期望'
之前,有a\n
个字符的奇数
什么可以是这个的正则表达式?
{
'key1': 'a
b',
'key2':'value2',
'key3':'value3'
}
答案 0 :(得分:0)
以下是使用replaceAll()
public class Program
{
public static void main(String[] args) {
String str = "{
'key1': 'a
b',
'key2':'value2',
'key3':'value3'
}";
str = str.replaceAll("a\nb", "a\\nb");
System.out.println(str);
}
}
修改:将replaceAll("\n", "\\n")
更改为replaceAll("a\nb", "a\\nb")