我有Map<String,Integer>
我希望使用以下格式[{text: String, count: Integer},{text: String2, count: Integer2},...]
我知道如何用python中的理解来做这些事情,但之前从未使用过Jackson,我现在需要起诉。
我这样做是为了将地图转换为Json
ObjectMapper mapper = new ObjectMapper();
try {
System.out.println(mapper.writeValueAsString(myMap));
//(looks like {"word":1,"word2":2,"word3":5}) (so I need to add a label text:
// before each word a label weight before each number and put each word/number
// block in a separate {})
} catch (JsonGenerationException e) {
e.printStackTrace();
} catch (JsonMappingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
答案 0 :(得分:2)
算法很简单:
Map<String, Integer>
转换为POJO实例列表。