
在Java应用中处理JSON数据是常见的任务,但当JSON结构复杂且需要进行数据聚合(如计算最小值、最大值或平均值)时,传统的反序列化方法可能变得繁琐。本教程将展示如何利用Josson库的强大转换能力,将一个嵌套的JSON数组转换为一个扁平化的POJO列表,同时计算每个元素的聚合统计量。
假设我们有一个包含词语及其在不同上下文中出现次数的嵌套JSON结构:
[
[
{
"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 POJO列表,其中min和max字段表示该词语在所有出现中的最小和最大计数:
public class Word {
private String text;
private Integer min;
private Integer max;
// Getters and Setters
public void setText(String text) {
this.text = text;
}
public void setMin(Integer min) {
this.min = min;
}
public void setMax(Integer max) {
this.max = max;
}
@Override
public String toString() {
return String.format("text=%s min=%d max=%d", text, min, max);
}
}例如,对于词语"china",我们期望得到 text="china", min=0, max=10。
虽然Jackson库在Java中进行JSON序列化和反序列化非常强大,但对于复杂的聚合和结构转换,它可能需要编写大量的自定义反序列化器。Josson库提供了一种声明式的方式来查询和转换JSON数据,极大地简化了这类操作。
首先,确保你的项目中包含了Josson和Jackson的依赖。如果你使用Maven,可以在pom.xml中添加:
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
<dependencies>
<dependency>
<groupId>com.octomix.josson</groupId>
<artifactId>josson</artifactId>
<version>1.3.1</version> <!-- 请检查最新版本 -->
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version> <!-- 请检查最新版本 -->
</dependency>
</dependencies>Josson的核心在于其强大的查询表达式。针对上述问题,我们需要执行以下步骤:
以下是实现这一转换的Josson表达式和Java代码:
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 JsonToPojoAggregator {
public static void main(String[] args) throws Exception {
String jsonString = "[" +
" [" +
" {\"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(jsonString);
// 2. 构建Josson查询表达式进行转换和聚合
// flatten(): 将嵌套数组扁平化
// group(word): 按 'word' 字段分组
// map(text:word, min:elements.min(count), max:elements.max(count)):
// - text:word 将当前分组的 'word' 值映射到 'text'
// - min:elements.min(count) 计算当前分组内所有元素的 'count' 字段的最小值,映射到 'min'
// - max:elements.max(count) 计算当前分组内所有元素的 'count' 字段的最大值,映射到 'max'
JsonNode node = josson.getNode(
"flatten()" +
".group(word)" +
".map(text:word, min:elements.min(count), max:elements.max(count))");
// 3. 使用Jackson ObjectMapper 将转换后的JsonNode转换为List<Word>
ObjectMapper objectMapper = new ObjectMapper();
List<Word> words = objectMapper.convertValue(node, new TypeReference<List<Word>>() {});
// 4. 打印结果
words.forEach(System.out::println);
}
}执行上述代码,将得到以下输出:
text=china min=0 max=10 text=kids min=0 max=3 text=music min=0 max=2
这正是我们期望的结果,每个词语的最小和最大出现次数都已正确计算并映射到Word POJO中。
通过本教程,我们学习了如何利用Josson库的强大功能,高效地将复杂的嵌套JSON结构转换为扁平化的Java POJO,并在转换过程中执行数据聚合。这种方法极大地简化了数据处理逻辑,提升了代码的可读性和可维护性。
以上就是从复杂JSON结构中提取并聚合数据到POJO的实践的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号