Jackson :loosing backslash from json string during deserialiastion

59 Views Asked by At

Unable to retain the backslash in the java object after deserialtion

I have below json {\"Num\":\"44566\677\"}"

When I deserialise this to object

I am loosing the backslash

Expected :6787\32433 Actual:678732433

This is what I tried

String json = "{\"num\":\"6787\32432"}";


Objectmapper obj = new Objectmapper();


obj.enable(ALLOW_BACKSLASH_ESCAPING_ANY_CAHATCTER);


obj.readvalue(json, new Typereferenxe<Map<String,String>>(){}));
1

There are 1 best solutions below

0
Michael Gantman On

Backslash itself should be escaped so try to add another backslash into your Json - change {\"Num\":\"44566\677\"} to {\"Num\":\"44566\\677\"}

Also, if you are interested you can use a simple JsonUtils class for serializing and deserializing simple JSON to/from POJO. JsonUtils is a thin wrapper over ObjectMapper class. your code would become a one liner:

Map<String, Object> map = JsonUtils.readObjectFromJsonString(json, Map.class);`

JsonUtils class comes with Open Source MjgntUtils java library written and maintained by me. Here is JsonUtils Javadoc. You can get the library as maven artifact or from Github - with source code and Javadoc