首頁 > Java > java教程 > Java中@JSONField物件欄位重新命名和順序問題怎麼解決

Java中@JSONField物件欄位重新命名和順序問題怎麼解決

王林
發布: 2023-04-18 15:28:03
轉載
1778 人瀏覽過

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",
    "legalPersonPerson":"4",#oo "
}

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_license_number":"2",
    "business_time":"6",
    "company_address":"5",
    "legal_person":"4",
    "merchant_name":"

#三、字段排序

1.我們輸出列印的json是這樣的

{

    "business_license_copy":"1",
    "business_license_number ":"2",

    "business_time":"6",
     "business_time":"6",
    "company_address":"5",
    "legal_person":"4",##"# ##}


我們想要按照一定的順序重新排序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_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參數之前執行結果


#加上ordinal參數之後執行結果Java中@JSONField物件欄位重新命名和順序問題怎麼解決

Java中@JSONField物件欄位重新命名和順序問題怎麼解決

####################################################################################

以上是Java中@JSONField物件欄位重新命名和順序問題怎麼解決的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:yisu.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板