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

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

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

依赖项

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

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

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

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);
    }
}

输出结果如下:

json 复制代码
{
  "id" : 1,
  "firstName" : "Ramesh",
  "lastName" : "Fadatare",
  "fullName" : "Ramesh Fadatare"
}

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

2. 使用@JsonProperty注解

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

json 复制代码
{
  "id" : 1,
  "first_name" : "Ramesh",
  "last_name" : "Fadatare",
  "full_name" : "Ramesh Fadatare"
}

User.java (带@JsonProperty注解)

java 复制代码
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

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);
    }
}

输出结果如下:

json 复制代码
{
  "id" : 1,
  "first_name" : "Ramesh",
  "last_name" : "Fadatare",
  "full_name" : "Ramesh Fadatare"
}

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

相关推荐
子非衣4 小时前
MySQL修改JSON格式数据示例
android·mysql·json
林的快手2 天前
伪类选择器
android·前端·css·chrome·ajax·html·json
css趣多多3 天前
初步安装和使用vant组件库,使用css变量定制vant主题样式 ,小程序的API Promise化,调用promise化之API
json
Web极客码3 天前
WordPress“更新失败,响应不是有效的JSON响应”问题的修复
json·github·wordpress
gywl5 天前
Spring Web MVC入门
spring·json·mvc·注解·cookie·session
运维小文6 天前
Mongodb数据管理
数据库·mongodb·json·非关系数据库·文档型数据库
玉阳软件yuyangdev_cn6 天前
铁塔电单车协议对接电单车TCP json协议对接成熟充电桩系统搭建低速充电桩TCP 接口规范
网络·tcp/ip·json·铁塔协议
m0_748232927 天前
SpringCloud系列教程:微服务的未来 (五)枚举处理器、JSON处理器、分页插件实现
spring cloud·微服务·json
SendSi7 天前
Unity使用反射进行Protobuf(CS/SC)协议,json格式
unity·json·游戏引擎
CURRY30_HJH7 天前
MYSQL直接在SQL提取json字符串中的内容-----将13位时间戳转换成标准的日期格式【记录SQL常用函数】
数据库·sql·json