Reputation: 679
I have a Map which contains another Map as follows:
private ObjectMapper mapper;
private Map<String,Map<String,Long>> indicatorsList;
How can I use Jackson in order to convert it to POJO?
This is what I was trying to do:
public Map<String,Map<String,Long>> calculateIndicators(List<indicatorsDAO> events){
Map<String,Map<String,Long>> indicatorsCountersMap=
events.stream().collect(
Collectors.groupingBy(
indicatorsDAO::getType,
Collectors.groupingBy(
indicatorsDAO::getLight,
Collectors.counting())
)
);
return mapper.convertValue(indicatorsCountersMap,Indicators.class);
}
This is Indicators class:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Indicators{
private Map<String,Map<String,Long>> indicatorsList;
}
The results of this conversion is: null
This is how the map should look like, just an example:
"indicatorsList": {
"Bulb": {
"Green": 2,
"Red": 3
},
"Led": {
"Red": 1
}
}
Upvotes: 3
Views: 1104
Reputation: 190
Try to use the below code snippet to convert your MAP object the JSON string.
ObjectMapper mapperObj = new ObjectMapper();
String jsonStr = StringUtils.EMPTY;
try {
jsonStr = mapperObj.writeValueAsString(<<MAP OBJECT>>);
}
catch (IOException e) {
e.printStackTrace();
}
Upvotes: 3