如何避免杰克逊逃避反斜杠?

时间:2015-10-29 13:10:16

标签: java json jackson

我将"this\'s my case!"设置为对象的字段。

致电writeAsString,输出为"this\\\'s my case!"

但我希望"this\'s my case!"

我怎样才能得到我想要的东西?

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;
        }
    }
}

输出上述代码:

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

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

1 个答案:

答案 0 :(得分:0)

如果将字符串xx更改为

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

你会得到理想的结果;

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