Reputation: 49
I have a hash map and i was added 2 values into that map. After that i want to convert to json, but it is showing as string after converting json using jackson. below is my code :
Map<String, Map<String, String>> attributes = new HashMap<String, Map<String, String>>();
Map<String, String> values = new HashMap<String, String>();
Map<String, String> metadata = new HashMap<String, String>();
metadata.put("type", test.getType().toString());
metadata.put("id", test.getId());
String metadataStr = metadata.toString();
String replaceStr = metadataStr.replace("=", ":");
values.put("metadata", replaceStr);
attributes.put("caption", values);
Here I am converting attributes to json like below :
String json = this.getObjectMapper().writeValueAsString(attributes);
{
"Test": {
"metadata": "{id:f600b8fa-77cf-4225-ba42-39135909c7ce, type:test}",
}
}
But i need like below :
{
"Test": {
"metadata": {
"id":"f600b8fa-77cf-4225-ba42-39135909c7ce",
"type":"test" }
}
}
Can any one help me out this. Thanks in Advance.
Upvotes: 0
Views: 147
Reputation: 255
The root of cause is in type of values and this line:
values.put("metadata", replaceStr);
Need to have:
Map<String, Map<String, Map<String, String>>> attributes = new HashMap<String, Map<String, Map<String, String>>>();
Map<String, Map<String, String>> values = new HashMap<String, Map<String, String>>();
Map<String, String> metadata = new HashMap<String, String>();
metadata.put("type", test.getType().toString());
metadata.put("id", test.getId());
values.put("metadata", metadata);
attributes.put("caption", values);
Upvotes: 1