
在实际的java开发中,我们经常需要处理来自外部服务或文件的不规则json数据。一个常见的场景是,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对象列表,每个对象代表一个唯一的单词及其在所有出现中的最小和最大计数:
public class Word {
private String text;
private Integer min;
private Integer max;
// Getters and Setters
}例如,对于“china”这个词,其count值分别为0、3、10,我们期望得到text="china", min=0, max=10。直接使用Jackson ObjectMapper进行反序列化难以实现这种聚合逻辑。这时,引入一个强大的JSON查询和转换库就显得尤为重要。
首先,我们定义目标POJO Word 类,它将承载聚合后的数据:
public class Word {
private String text;
private Integer min;
private Integer max;
// 构造函数、Getter和Setter方法
public Word() {} // Jackson需要无参构造函数
public void setText(String text) {
this.text = text;
}
public void setMin(Integer min) {
this.min = min;
}
public void setMax(Integer max) {
this.max = max;
}
public String getText() {
return text;
}
public Integer getMin() {
return min;
}
public Integer getMax() {
return max;
}
@Override
public String toString() {
return String.format("text=%s min=%d max=%d", text, min, max);
}
}为了实现复杂的聚合和结构转换,我们引入Josson库。Josson是一个强大的Java JSON处理器,它允许我们使用类似于SQL的查询语法对JSON数据进行筛选、转换和聚合。
在使用Josson之前,请确保在您的项目中添加相应的Maven或Gradle依赖。
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
Maven:
<dependency>
<groupId>com.github.octomix</groupId>
<artifactId>josson</artifactId>
<version>1.3.0</version> <!-- 请检查最新版本 -->
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.17.0</version> <!-- 请检查最新版本 -->
</dependency>Gradle:
implementation 'com.github.octomix:josson:1.3.0' // 请检查最新版本 implementation 'com.fasterxml.jackson.core:jackson-databind:2.17.0' // 请检查最新版本
Josson通过其查询语言提供了一种简洁的方式来处理上述转换。以下是实现步骤和关键的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) {
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}" +
" ]" +
"]";
try {
// 1. 使用Josson加载JSON字符串
Josson josson = Josson.fromJsonString(jsonString);
// 2. 执行Josson查询,转换并聚合数据
JsonNode transformedNode = josson.getNode(
"flatten()" +
".group(word)" +
".map(text:word, min:elements.min(count), max:elements.max(count))"
);
// 3. 使用Jackson ObjectMapper将Josson转换后的JsonNode反序列化为List<Word>
ObjectMapper objectMapper = new ObjectMapper();
List<Word> words = objectMapper.convertValue(transformedNode, new TypeReference<List<Word>>() {});
// 4. 打印结果
words.forEach(System.out::println);
} catch (Exception e) {
e.printStackTrace();
}
}
}输出结果:
text=china min=0 max=10 text=kids min=0 max=3 text=music min=0 max=2
通过上述方法,我们成功地将一个嵌套且需要聚合的复杂JSON结构,高效地转换并映射到了一个简洁的Java POJO列表,这在数据处理和系统集成中具有广泛的应用价值。
以上就是利用Josson和Jackson处理复杂JSON数据:聚合与POJO映射的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号