首页 > Java > java教程 > Java中@JSONField对象字段重命名和顺序问题怎么解决

Java中@JSONField对象字段重命名和顺序问题怎么解决

王林
发布: 2023-04-18 15:28:03
转载
1779 人浏览过

Java对象转Json,@JSONField对象字段重命名和顺序

一、引入maven依赖

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.66</version>
        </dependency>
登录后复制

二、字段重命名

1.创建一个测试实体

import lombok.Data; 
import java.io.Serializable;
 
/**
 * @类名 WeChatBusinessLicenseInfo
 * @描述 营业执照/登记证书信息(测试用)
 * @版本 1.0
 * @创建人 XuKang
 * @创建时间 2021/12/24 10:43
 **/
@Data
public class LkWeChatBusinessLicenseInfo implements Serializable {
    private static final long serialVersionUID = 1582941630439552458L;
    private String businessLicenseCopy;
    private String businessLicenseNumber;
    private String merchantName;
    private String legalPerson;
    private String companyAddress;
    private String businessTime;
    public LkWeChatBusinessLicenseInfo(){
        this.businessLicenseCopy = "1";
        this.businessLicenseNumber = "2";
        this.merchantName = "3";
        this.legalPerson = "4";
        this.companyAddress = "5";
        this.businessTime = "6";
    }
}
登录后复制

2.将实体转换为json字符串,看看未转换前的效果

System.out.println(JSONObject.toJSONString(new LkWeChatBusinessLicenseInfo()));
登录后复制
登录后复制

{
"businessLicenseCopy":"1",
"businessLicenseNumber":"2",
"businessTime":"6",
"companyAddress":"5",
"legalPerson":"4",
"merchantName":"3"
}

3.我们要转换为带下划线的key,例如把businessLicenseCopy转换为business_license_copy

我们需要修改实体,加上注解@JSONField

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import java.io.Serializable;
 
/**
 * @类名 WeChatBusinessLicenseInfo
 * @描述 营业执照/登记证书信息(测试用)
 * @版本 1.0
 * @创建人 XuKang
 * @创建时间 2021/12/24 10:43
 **/
@Data
public class LkWeChatBusinessLicenseInfo implements Serializable {
    private static final long serialVersionUID = 1582941630439552458L;
    @JSONField(name = "business_license_copy")
    private String businessLicenseCopy;
 
    @JSONField(name = "business_license_number")
    private String businessLicenseNumber;
 
    @JSONField(name = "merchant_name")
    private String merchantName;
 
    @JSONField(name = "legal_person")
    private String legalPerson;
 
    @JSONField(name = "company_address")
    private String companyAddress;
 
    @JSONField(name = "business_time")
    private String businessTime;
 
    public LkWeChatBusinessLicenseInfo(){
        this.businessLicenseCopy = "1";
        this.businessLicenseNumber = "2";
        this.merchantName = "3";
        this.legalPerson = "4";
        this.companyAddress = "5";
        this.businessTime = "6";
    }
}
登录后复制

4.加上注解后打印转换后的json

System.out.println(JSONObject.toJSONString(new LkWeChatBusinessLicenseInfo()));
登录后复制
登录后复制

{
"business_license_copy":"1",
"business_license_number":"2",
"business_time":"6",
"company_address":"5",
"legal_person":"4",
"merchant_name":"3"
}

三、字段排序

1.我们输出打印的json是这样的

{
"business_license_copy":"1",
"business_license_number":"2",
"business_time":"6",
"company_address":"5",
"legal_person":"4",
"merchant_name":"3"
}

我们想按照一定的顺序重新排序key

2.在@JSONField注解加上排序ordinal

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import java.io.Serializable;
 
/**
 * @类名 WeChatBusinessLicenseInfo
 * @描述 营业执照/登记证书信息(测试用)
 * @版本 1.0
 * @创建人 XuKang
 * @创建时间 2021/12/24 10:43
 **/
@Data
public class LkWeChatBusinessLicenseInfo implements Serializable {
 
    private static final long serialVersionUID = 1582941630439552458L;
 
    @JSONField(name = "business_license_copy",ordinal = 1)
    private String businessLicenseCopy;
 
    @JSONField(name = "business_license_number",ordinal = 2)
    private String businessLicenseNumber;
 
    @JSONField(name = "merchant_name",ordinal = 3)
    private String merchantName;
 
    @JSONField(name = "legal_person",ordinal = 4)
    private String legalPerson;
 
    @JSONField(name = "company_address",ordinal = 5)
    private String companyAddress;
 
    @JSONField(name = "business_time",ordinal = 6)
    private String businessTime;
 
    public LkWeChatBusinessLicenseInfo(){
        this.businessLicenseCopy = "1";
        this.businessLicenseNumber = "2";
        this.merchantName = "3";
        this.legalPerson = "4";
        this.companyAddress = "5";
        this.businessTime = "6";
    }
}
登录后复制

3.输出打印转换后的实体:

System.out.println(JSONObject.toJSONString(new LkWeChatBusinessLicenseInfo()));
{
"business_license_copy":"1",
"business_license_number":"2",
"merchant_name":"3",
"legal_person":"4",
"company_address":"5",
"business_time":"6"
}

小结:重命名除@JSONField,还有@JsonProperty、@SerializedName;@JsonProperty主要用于入参转换,和Json字符串序列化为Java对象;@SerializedName 改变了默认序列化和默认反序列化的字段取值;

@JSONField注解常用的使用场景

应用场景:

当我们在与前端进行交互时,前端想要的字段与我们提供的字段名不同,这时候一种解决方案是修改实体类,但如果该实体类应用的比较多,那改起来的代价太大,因此,可以使用注解@JSONField来实现替换效果,用法如下:

@JSONField(name = "size_new")
private int size;
登录后复制

一、JSON内容与实体类,@JSONField常规写法

JSON(与下述JSON字符串内容一致)

{
    size: 5,
    weight: 10,
    colour: "red"
}
登录后复制

实体类(AppleDO.java)

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
public class AppleDO {
    @JSONField(name = "size_new")
    private int size;
    @JSONField(name = "weight_new")
    private int weight;
    @JSONField(name = "colour_new")
    private String colour;
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public int getWeight() {
        return weight;
    }
    public void setWeight(int weight) {
        this.weight = weight;
    }
    public String getColour() {
        return colour;
    }
    public void setColour(String colour) {
        this.colour = colour;
    }
}
登录后复制

二、JSON字符串转对应Java对象

执行代码

public static void main(String[] args) {
    String json = "{\n" +
        "    size_new: 5,\n" +
        "    weight_new: 10,\n" +
        "    colour_new: \"red\",\n" +
        "}";
    AppleDO appleDO = JSON.parseObject(json, AppleDO.class);
    System.out.println(appleDO.getSize());
    System.out.println(appleDO.getWeight());
    System.out.println(appleDO.getColour());
}
登录后复制

运行结果

Java中@JSONField对象字段重命名和顺序问题怎么解决

三、支持序列化和反序列化

源码中的序列化和反序列化默认值均为true,则默认情况下是允许该字段序列化和反序列化的,如下:

boolean serialize() default true;
boolean deserialize() default true;
登录后复制

使用方法(以下不支持序列化,支持反序列化)

@JSONField(name = "size_new", serialize = false, deserialize = true)
private int size;
登录后复制

当我们的某些字段为空值时,我们仍希望将此字段返回到前端(该配置可以返回带有空字段的字符串,但是当字段为基本数据类型时无效,须将其转换为包装类)

@JSONField(serialzeFeatures= SerializerFeature.WriteMapNullValue)
登录后复制

四、指定字段顺序

将Java对象转换为JSON格式,转换后的字段顺序会根据首字母来排序,亦可通过如下方式来指定字段顺序:

@JSONField(name = "size_new", ordinal = 3)
private int size;
@JSONField(name = "weight_new", ordinal = 1)
private int weight;
@JSONField(name = "colour_new", ordinal = 2)
private String colour;
登录后复制

执行代码

AppleDO apple = new AppleDO();
apple.setSize(6);
apple.setWeight(12);
apple.setColour("green");
String appleStr = JSON.toJSONString(apple);
System.out.println(appleStr);
登录后复制

加ordinal参数之前运行结果


Java中@JSONField对象字段重命名和顺序问题怎么解决

加ordinal参数之后运行结果

Java中@JSONField对象字段重命名和顺序问题怎么解决

以上是Java中@JSONField对象字段重命名和顺序问题怎么解决的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:yisu.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板