当前位置: 首页 > news >正文

Jackson - 序列化时更改字段名称

在这个简短的教程中,我将向您展示如何在序列化时更改字段名称以映射到另一个JSON属性。

Jackson库提供了@JsonProperty注解,用于改变序列化JSON中的属性名称。

依赖项

首先,在pom.xml文件中添加以下依赖项:

<dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.9.8</version>
</dependency>

此依赖项还会自动引入以下库到类路径中:

  • jackson-annotations-2.9.8.jar
  • jackson-core-2.9.8.jar
  • jackson-databind-2.9.8.jar

始终建议使用Maven中央仓库中的最新版本。

更改字段名进行序列化

1. 不使用@JsonProperty注解

我们先创建一个简单的Java类,并测试它而不添加@JsonProperty注解。

User.java

package net.javaguides.jackson.annotations;public class User {public int id;private String firstName;private String lastName;private String fullName;public User(int id, String firstName, String lastName, String fullName) {this.id = id;this.firstName = firstName;this.lastName = lastName;this.fullName = fullName;}// Getters and Setters
}

使用主方法测试上述代码:

JsonPropertyAnnotationTest.java

package net.javaguides.jackson.annotations;import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;public class JsonPropertyAnnotationTest {public static void main(String[] args) throws IOException {ObjectMapper mapper = new ObjectMapper();mapper.enable(SerializationFeature.INDENT_OUTPUT);User bean = new User(1, "Ramesh", "Fadatare", "Ramesh Fadatare");String result = mapper.writeValueAsString(bean);System.out.println(result);}
}

输出结果如下:

{"id" : 1,"firstName" : "Ramesh","lastName" : "Fadatare","fullName" : "Ramesh Fadatare"
}

如你所见,如果不使用@JsonProperty注解,那么属性名将与类中的getter和setter方法相同。

2. 使用@JsonProperty注解

现在让我们给User类的字段添加@JsonProperty注解,来自定义输出,使得JSON格式如下所示:

{"id" : 1,"first_name" : "Ramesh","last_name" : "Fadatare","full_name" : "Ramesh Fadatare"
}

User.java (带@JsonProperty注解)

package net.javaguides.jackson.annotations;import com.fasterxml.jackson.annotation.JsonProperty;public class User {public int id;@JsonProperty("first_name")private String firstName;@JsonProperty("last_name")private String lastName;@JsonProperty("full_name")private String fullName;public User(int id, String firstName, String lastName, String fullName) {this.id = id;this.firstName = firstName;this.lastName = lastName;this.fullName = fullName;}// Getters and Setters
}

再次使用主方法测试修改后的代码:

JsonPropertyAnnotationTest.java

package net.javaguides.jackson.annotations;import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;public class JsonPropertyAnnotationTest {public static void main(String[] args) throws IOException {ObjectMapper mapper = new ObjectMapper();mapper.enable(SerializationFeature.INDENT_OUTPUT);User bean = new User(1, "Ramesh", "Fadatare", "Ramesh Fadatare");String result = mapper.writeValueAsString(bean);System.out.println(result);}
}

输出结果如下:

{"id" : 1,"first_name" : "Ramesh","last_name" : "Fadatare","full_name" : "Ramesh Fadatare"
}

通过使用@JsonProperty注解,您可以轻松地控制序列化过程中生成的JSON属性名称,从而满足特定的需求或符合外部API的要求。

http://www.lryc.cn/news/498959.html

相关文章:

  • 策略模式实战 - 猜拳游戏
  • AWS ECS Task 添加 Prometheus 监控采集配置详细指南
  • 5. 一分钟读懂“工厂方法模式”
  • 基于 AutoFlow 快速搭建基于 TiDB 向量搜索的本地知识库问答机器人
  • C语言学习:速通指针(2)
  • windows 上ffmpeg编译好的版本选择
  • Java设计模式笔记(二)
  • Vue CLI的作用
  • 短视频矩阵系统开发|技术源代码部署
  • Erlang socket编程(二)
  • 工业检测基础-线扫相机和面阵相机参数及应用
  • 【无标题】建议用坚果云直接同步zotero,其他方法已经过时,容易出现bug
  • 基于STM32设计的智能宠物喂养系统(华为云IOT)_273
  • cesium truf 利用缓冲如何将一个点缓冲成一个方形
  • HarmonyOS 5.0应用开发——Ability与Page数据传递
  • 【推荐算法】推荐系统的评估
  • 鸿蒙:实现类似Android.9图的图片资源呈现
  • ros2人脸检测
  • Pillow:强大的Python图像处理库
  • 微信小程序uni-app+vue3实现局部上下拉刷新和scroll-view动态高度计算
  • 为什么类 UNIX 操作系统通常内置编译器?为什么 Windows 更倾向于直接使用二进制文件?
  • 吉林大学23级数据结构上机实验(第7周)
  • 实验13 使用预训练resnet18实现CIFAR-10分类
  • 【开发文档】资源汇总,持续更新中......
  • 【k8s实践】 创建第一个Pod(Nginx)
  • 盘古大模型实战
  • Python subprocess.run 使用注意事项,避免出现list index out of range
  • 包管理器npm,cnpm,yarn和pnpm
  • 树莓派4B使用opencv读取摄像头配置指南
  • Spring Boot 进阶话题:部署