是用struts 的json插件进行序列化

代码如下:
@test
public void test_json() throws jsonexception {
map<string, string> map = new hashmap<string, string>();
map.put("name", "whua\"ng");
string result = jsonutil.serialize(map);
system.out.println(result);
string source = "{\"result\":\"_jsonplaceholder\"}";
string result2=source.replaceall("\"" + "_jsonplaceholder" + "\"", result);
system.out.println(result2);
}
运行结果如下:
{"name":"whua\"ng"}
{"result":{"name":"whua"ng"}}
是用json解析时报错,{"result":{"name":"whua"ng"}} 不是合法的json字符串。
经过反复看,发现whua\"ng中的斜杠没有了!!!!
问题出在replaceall
解决方法如下
(添加).replaceall("\\\\", "\\\\\\\\")
string result2 = source.replaceall("\"" + "_jsonplaceholder" + "\"",
result.replaceall("\\\\", "\\\\\\\\"));