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的要求。

相关推荐
CSCN新手听安21 小时前
【linux】网络基础(三)TCP服务端网络版本计算器的优化,Json的使用,服务器守护进程化daemon,重谈OSI七层模型
linux·服务器·网络·c++·tcp/ip·json
bloglin9999921 小时前
Qwen3-32B报错Invalid json output:{“type“: “1“}For troubleshooting, visit
llm·json
Trouvaille ~1 天前
【Linux】应用层协议设计实战(二):Jsoncpp序列化与完整实现
linux·运维·服务器·网络·c++·json·应用层
剩下了什么1 天前
MySQL JSON_SET() 函数
数据库·mysql·json
梦帮科技2 天前
Node.js配置生成器CLI工具开发实战
前端·人工智能·windows·前端框架·node.js·json
数据知道2 天前
PostgreSQL实战:详解如何用Python优雅地从PG中存取处理JSON
python·postgresql·json
缘空如是2 天前
基础工具包之JSON 工厂类
java·json·json切换
墨痕诉清风3 天前
CVS文件转Json格式
json·python3·cvs
数研小生3 天前
1688商品列表API:高效触达批发电商海量商品数据的技术方案
大数据·python·算法·信息可视化·json
devmoon3 天前
快速了解兼容 Ethereum 的 JSON-RPC 接口
开发语言·网络·rpc·json·区块链·智能合约·polkadot