您好,登錄后才能下訂單哦!
本篇內容介紹了“Java中@JSONField對象字段重命名和順序問題怎么解決”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
<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(name = "size_new") private int size;
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; } }
執行代碼
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()); }
運行結果
源碼中的序列化和反序列化默認值均為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對象字段重命名和順序問題怎么解決”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。