我有一个包含以下其他地图的地图:
private ObjectMapper mapper;
private Map<String,Map<String,Long>> indicatorsList;
如何使用Jackson将其转换为POJO?
这就是我想要做的:
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);
}
这是指标类:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Indicators{
private Map<String,Map<String,Long>> indicatorsList;
}
此转换的结果为:null
这是地图的外观,仅举一个例子:
"indicatorsList": {
"Bulb": {
"Green": 2,
"Red": 3
},
"Led": {
"Red": 1
}
}
答案 0 :(得分:3)
尝试使用以下代码片段将MAP对象转换为JSON字符串。
ObjectMapper mapperObj = new ObjectMapper();
String jsonStr = StringUtils.EMPTY;
try {
jsonStr = mapperObj.writeValueAsString(<<MAP OBJECT>>);
}
catch (IOException e) {
e.printStackTrace();
}