将含词频的嵌套JSON转换为带min/max的Java Word对象
使用Jackson处理嵌套JSON并转换为自定义POJO
完全可以用Jackson实现你的需求,步骤分三部分:先把JSON反序列化为中间数据结构,再统计每个单词的最小/最大次数,最后生成目标POJO。
1. 定义中间POJO映射JSON结构
JSON里的每个{"word":"xxx", "count":n}可以用一个简单的POJO来映射:
public class WordCount { private String word; private Integer count; // 必须生成getter和setter,Jackson依赖它们完成反序列化 public String getWord() { return word; } public void setWord(String word) { this.word = word; } public Integer getCount() { return count; } public void setCount(Integer count) { this.count = count; } }
2. 用Jackson反序列化JSON
把嵌套JSON转换成List<List<WordCount>>类型的对象:
import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.util.List; public class JsonToWordProcessor { public static void main(String[] args) throws IOException { // 你的JSON字符串,实际场景可以从文件/网络读取 String jsonContent = "[[{\"word\":\"china\",\"count\":0},{\"word\":\"kids\",\"count\":1},{\"word\":\"music\",\"count\":0}],[{\"word\":\"china\",\"count\":3},{\"word\":\"kids\",\"count\":0},{\"word\":\"music\",\"count\":2}],[{\"word\":\"china\",\"count\":10},{\"word\":\"kids\",\"count\":3},{\"word\":\"music\",\"count\":2}]]"; ObjectMapper objectMapper = new ObjectMapper(); // 构造嵌套列表的类型,告诉Jackson如何反序列化 List<List<WordCount>> nestedWordCounts = objectMapper.readValue( jsonContent, objectMapper.getTypeFactory().constructCollectionType( List.class, objectMapper.getTypeFactory().constructCollectionType(List.class, WordCount.class) ) ); } }
3. 统计并生成目标Word对象
遍历反序列化后的嵌套列表,用Map分组统计每个单词的最小、最大次数,最后转换成List<Word>:
import java.util.ArrayList; import java.util.HashMap; import java.util.Map; // 接上面main方法的代码 Map<String, int[]> wordStatsMap = new HashMap<>(); // 遍历所有WordCount对象,更新统计值 for (List<WordCount> wordCountList : nestedWordCounts) { for (WordCount wordCount : wordCountList) { String word = wordCount.getWord(); int count = wordCount.getCount(); // 如果单词是第一次出现,初始化min和max为当前count wordStatsMap.computeIfAbsent(word, k -> new int[]{count, count}); int[] currentStats = wordStatsMap.get(word); // 更新最小值 if (count < currentStats[0]) { currentStats[0] = count; } // 更新最大值 if (count > currentStats[1]) { currentStats[1] = count; } } } // 转换为目标Word对象列表 List<Word> wordList = new ArrayList<>(); for (Map.Entry<String, int[]> entry : wordStatsMap.entrySet()) { Word word = new Word(); word.setText(entry.getKey()); word.setMin(entry.getValue()[0]); word.setMax(entry.getValue()[1]); wordList.add(word); } // 验证结果 for (Word w : wordList) { System.out.printf("word.text = \"%s\"\nword.min = %d;\nword.max = %d;\n\n", w.getText(), w.getMin(), w.getMax()); }
补充说明
- 你的
Word类需要添加getter和setter方法,否则无法正常赋值(或者用Lombok的@Data注解自动生成):
public class Word { private String text; private Integer min; private Integer max; // 添加getter和setter public String getText() { return text; } public void setText(String text) { this.text = text; } public Integer getMin() { return min; } public void setMin(Integer min) { this.min = min; } public Integer getMax() { return max; } public void setMax(Integer max) { this.max = max; } }
- 实际项目中不要直接抛出
IOException,建议用try-catch块捕获处理;如果JSON来自文件,可以用objectMapper.readValue(new File("your-file.json"), ...)读取。
内容的提问来源于stack exchange,提问作者Mattias Törnqvist
相关产品推荐
相关产品推荐

