I have created an application for converting the HashMap object to String, its working fine, The problem which i am facing is that i want to convert the HashMap string again back to HasMap object, when i tried that by using the following code , I am getting exception as shown below
Unexpected character ('u' (code 117)): was expecting double-quote to start field name
Can anyone please tell me some solution for this
My code is given below
Map<String,Object> map = new HashMap<String,Object>();
map.put("userVisible", true);
map.put("userId", "1256");
ObjectMapper mapper = new ObjectMapper();
try {
map = mapper.readValue(map.toString(), new TypeReference<HashMap<String,Object>>(){});
System.out.println(map.get("userId"));
} catch (Exception e) {
e.printStackTrace();
}
Update 1
As suggested by @chrylis I have used Feature.ALLOW_UNQUOTED_FIELD_NAMES like as shown below, but now i am getting the following exception
Unexpected character ('=' (code 61)): was expecting a colon to separate field name and value
Updated Code
Map<String,Object> map = new HashMap<String,Object>();
map.put("userVisible", true);
map.put("userId", "1256");
ObjectMapper mapper = new ObjectMapper();
mapper.configure(Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
try {
map = mapper.readValue(map.toString(), new TypeReference<HashMap<String,Object>>(){});
System.out.println(map.get("userId"));
} catch (Exception e) {
e.printStackTrace();
}
String mapStr = mapper.writeValueAsString(map);