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

相关推荐
tan77º1 天前
【项目】分布式Json-RPC框架 - 项目介绍与前置知识准备
linux·网络·分布式·网络协议·tcp/ip·rpc·json
Yn3122 天前
在 Python 中使用 json 模块的完整指南
开发语言·python·json
陈涛5754 天前
5个最好用的 JSON 工具推荐:让数据处理变得简单高效
json
bkspiderx5 天前
pb2json.hpp 文档:Protobuf 与 JSON 通用转换工具类
json·protobuf·protobuf与json转换
万粉变现经纪人6 天前
何解决PyCharm中pip install安装Python报错ModuleNotFoundError: No module named ‘json’问题
python·pycharm·json·beautifulsoup·scikit-learn·matplotlib·pip
晨欣6 天前
orjson 与 json:实战对比与选型指南(含示例)(GPT-5 回答)
gpt·json
Pi_Qiu_7 天前
Python初学者笔记第二十二期 -- (JSON数据解析)
笔记·python·json
mon_star°7 天前
有趣的 npm 库 · json-server
前端·npm·json
ID_180079054737 天前
淘宝拍立淘按图搜索API接口功能详细说明
大数据·python·json·图搜索算法
cypking7 天前
vue excel转json功能 xlsx
vue.js·json·excel