如果我尝试将已经包含反斜杠的字符串转换为字节数组,然后将该字节数组更改回字符串,则使用Jacksons的ObjectMapper,输出包含一个额外的反斜杠。以下代码显示了问题
public static void main(String[] args) throws JsonProcessingException, UnsupportedEncodingException {
ObjectMapper mapper = new ObjectMapper();
Map<String, String> data = new HashMap<>();
data.put("id", "Test\\String");
//output: data[id] that has single backslash: Test\String
System.out.println("data[id] that has single backslash: " + data.get("id"));
byte[] dataInBytes = mapper.writeValueAsBytes(data);
//output: data in bytes that contains double backslash: {"id":"Test\\String"}
System.out.println("data in bytes that contains double backslash: " + new String(dataInBytes, "UTF-8"));
}
有没有办法配置ObjectMapper,使maper#writeValueAsBytes方法不会将字符串Test \ String更改为Test \\ String?有没有其他方法使用Jackson / ObjectMapper来实现这一目标?
提前致谢!!
答案 0 :(得分:1)