简体   繁体   English

如何避免杰克逊转义反斜线?

[英]how to avoid jackson escaping backslash?

I set "this\\'s my case!" 我设置"this\\'s my case!" to an object's field. 到对象的字段。

Calling writeAsString , output is "this\\\\\\'s my case!" 调用writeAsString ,输出为"this\\\\\\'s my case!" .

But I expect "this\\'s my case!" 但是我希望"this\\'s my case!" .

How can I get what I want ? 我如何得到想要的东西?

public class CommonUtils {
    public static ObjectMapper objectMapper = new ObjectMapper();
    public static ObjectMapper getObjectMapperInstance(){
        return objectMapper;
    }

    public static void main(String[] args) throws IOException {
        CommonUtils commonUtils = new CommonUtils();
        commonUtils.test();
    }
    public void test() throws IOException {
        List<MyObject> list = new ArrayList<MyObject>();
        String xx = "page://list?params={\"city\"}";
        list.add(new MyObject(1,xx));
    }
    class MyObject{

        public MyObject(int tag,String str){
            this.tag = tag;
            this.str = str;
        }
        int tag;
        String str;

        public int getTag() {
            return tag;
        }

        public void setTag(int tag) {
            this.tag = tag;
        }

        public String getStr() {
            return str;
        }

        public void setStr(String str) {
            this.str = str;
        }
    }
}

Output of above code: 上面代码的输出:

[ { "tag" : 1, "str" : "page://list?params={\\\\"city\\\\"}] [{“ tag”:1,“ str”:“ page:// list?params = {\\\\” city \\\\“}]

What I want is: [ { "tag" : 1, "str" : "page://list?params={\\"city\\"}] 我想要的是:[{“ tag”:1,“ str”:“ page:// list?params = {\\” city \\“}]

If you Change the String xx to 如果将字符串xx更改为

String xx = "page://list?params={\\\"city\\\"}"; //page://list?params={\"city\"}

You will get desired result; 您将获得理想的结果;

[ { "tag" : 1, "str" : "page://list?params={\"city\"}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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