
在实际的软件开发中,我们经常会遇到结构复杂、层次嵌套的json数据。例如,一个场景是需要从一个包含多层数组和对象的json中,提取出某个元素的统计信息,如其在所有记录中的最小和最大出现次数。传统上,这可能需要编写复杂的循环逻辑,或者自定义jackson反序列化器,这无疑增加了开发难度和代码量。
考虑以下JSON结构,它是一个包含多个内部数组的数组,每个内部数组又包含多个带有word和count字段的对象:
[
[
{"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}
]
]我们的目标是将其转换为一个扁平化的Java对象列表,每个对象代表一个单词,并包含该单词在所有记录中的最小和最大出现次数。例如,对于单词"china",我们希望得到min=0,max=10。
为此,我们定义一个简单的POJO类Word:
public class Word {
private String text;
private Integer min;
private Integer max;
// Getters and Setters
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; }
@Override
public String toString() {
return String.format("text=%s min=%d max=%d", text, min, max);
}
}解决此类问题的关键在于对原始JSON数据进行预处理,将其转换为Jackson可以直接反序列化的结构。Josson是一个强大的Java库,专为JSON查询和转换设计,它提供了一种类似SQL的表达式语言来操作JSON数据。
立即学习“Java免费学习笔记(深入)”;
首先,我们需要在项目中引入Josson库的依赖。如果使用Maven,可以在pom.xml中添加:
<dependency>
<groupId>com.github.octomix</groupId>
<artifactId>josson</artifactId>
<version>1.3.0</version> <!-- 请替换为最新稳定版本 -->
</dependency>接下来,我们将使用Josson的查询能力来转换原始JSON。转换的核心思想是:
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
以下是使用Josson进行数据转换的代码示例:
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.octomix.josson.Josson;
import java.util.List;
public class JsonDataProcessor {
public static void main(String[] args) throws Exception {
String jsonInput = "[" +
" [" +
" {\"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}" +
" ]" +
"]";
// 1. 使用Josson加载JSON字符串
Josson josson = Josson.fromJsonString(jsonInput);
// 2. 构建Josson查询表达式进行数据转换
// flatten(): 将所有嵌套数组扁平化为单个数组
// group(word): 按 'word' 字段进行分组
// map(text:word, min:elements.min(count), max:elements.max(count)):
// 映射为新结构,其中 text 取 word 值,min 取分组内 count 的最小值,max 取分组内 count 的最大值
JsonNode transformedNode = josson.getNode(
"flatten()" +
".group(word)" +
".map(text:word, min:elements.min(count), max:elements.max(count))"
);
// 3. 使用Jackson ObjectMapper将转换后的JsonNode反序列化为POJO列表
ObjectMapper objectMapper = new ObjectMapper();
List<Word> words = objectMapper.convertValue(transformedNode, new TypeReference<List<Word>>() {});
// 4. 打印结果
words.forEach(System.out::println);
}
}Josson查询表达式解析:
执行上述代码,将得到以下输出:
text=china min=0 max=10 text=kids min=0 max=3 text=music min=0 max=2
这正是我们期望的结果,每个单词的最小和最大出现次数都被正确计算并映射到了Word对象中。
综上所述,利用Josson库进行JSON预处理,再结合Jackson进行POJO反序列化,是处理Java中复杂JSON数据转换和统计聚合的一种高效且推荐的方法。它将数据转换逻辑从Java代码中抽象出来,使得代码更清晰、更易维护。
以上就是Java JSON数据处理:利用Josson库高效提取嵌套结构中的统计信息的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号