是用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("\\\\", "\\\\\\\\"));