zapfen1 :
I am trying to find an easy way to deserialize the following JSON to an OpeningHours Object containing a Map<String, List<String>>
which contains the days as keys and the opening hours as a list.
I am using Jackson and created my own deserializer @JsonDeserialize(using = MyDeserializer.class)
The problem is that I need to check which of the different JSON nodes is present to set the keys for the map.
Is there an easy alternative solution?
{
"OpeningHours": {
"Days": {
"Monday": {
"string": [
"09:00-13:00",
"13:30-18:00"
]
},
"Tuesday": {
"string": [
"09:00-13:00",
"13:30-18:00"
]
}
}
}
}
Sharon Ben Asher :
Jackson can deserialize any Json into Map<String, Object>
, that may contain nested maps for nested json objects. all that is needed is casting:
String json = ...
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> openingHours = (Map<String, Object>)mapper.readValue(json, Map.class);
Map<String, List<String>> days = (Map<String, List<String>>)((Map<String, Object>)openingHours.get("OpeningHours")).get("Days");
System.out.println(days);
output:
{Monday={string=[09:00-13:00, 13:30-18:00]}, Tuesday={string=[09:00-13:00, 13:30-18:00]}}
Guess you like
Origin http://10.200.1.11:23101/article/api/json?id=418282&siteId=1