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

相关推荐
程序新视界12 小时前
一篇文章详解你不知道的MySQL JSON数据类型
数据库·mysql·json
数据知道14 小时前
Go基础:json文件处理详解(标准库`encoding/json`)
开发语言·后端·golang·json·go语言
广都--编程每日问1 天前
deepseek 的对话json导出成word和pdf
pdf·json·word·deepseek·exprot
包达叔3 天前
仿NewLife的XmlConfig类实现Json配置文件
c#·json·newlife
爱吃香蕉的阿豪5 天前
.NET Core 中 System.Text.Json 与 Newtonsoft.Json 深度对比:用法、性能与场景选型
数据库·json·.netcore
千叶寻-5 天前
package.json详解
前端·vue.js·react.js·webpack·前端框架·node.js·json
睿麒6 天前
鸿蒙app开发中 拿到json文件数据进行动画的播放
json
1314lay_10076 天前
使用HTTP接口,对接外围系统,封装调用跟推送类和动态转换json,动态编程和动态调用函数
http·json·abap
老坛程序员7 天前
抓包解析MCP协议:基于JSON-RPC的MCP host与MCP server的交互
人工智能·网络协议·rpc·json·交互
ZoeLandia7 天前
Vue 项目 JSON 在线编辑、校验如何选?
前端·vue.js·json