简体   繁体   中英

Jackson: customization escaping of double quotes while serialize an object to json

By default jackson escape double quotes by backslash: \\". So the json object looks like

{"title": "Testing \"double quotes\""}

I want to escape double quotes by two backslashes: \\\\". Like this

{"title": "Testing \\"double quotes\\""}

How to customize jackson serialization strategy for double quotes?

This will produce invalid JSON, so Jackson isn't going to help you do that. Assuming that you have no reverse solidus in the member names, you can just do it after outputting the actual string value:

String invalid = mapper.writeValueAsString(object).replace("\\", "\\\\");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM