Java對(duì)象轉(zhuǎn)Json,關(guān)于@JSONField對(duì)象字段重命名和順序問(wèn)題
Java對(duì)象轉(zhuǎn)Json,@JSONField對(duì)象字段重命名和順序
一、引入maven依賴
? ? ? ? <dependency> ? ? ? ? ? ? <groupId>com.alibaba</groupId> ? ? ? ? ? ? <artifactId>fastjson</artifactId> ? ? ? ? ? ? <version>1.2.66</version> ? ? ? ? </dependency>
二、字段重命名
1.創(chuàng)建一個(gè)測(cè)試實(shí)體
import lombok.Data;? import java.io.Serializable; ? /** ?* @類名 WeChatBusinessLicenseInfo ?* @描述 營(yíng)業(yè)執(zhí)照/登記證書(shū)信息(測(cè)試用) ?* @版本 1.0 ?* @創(chuàng)建人 XuKang ?* @創(chuàng)建時(shí)間 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.將實(shí)體轉(zhuǎn)換為json字符串,看看未轉(zhuǎn)換前的效果
System.out.println(JSONObject.toJSONString(new LkWeChatBusinessLicenseInfo()));
{
"businessLicenseCopy":"1",
"businessLicenseNumber":"2",
"businessTime":"6",
"companyAddress":"5",
"legalPerson":"4",
"merchantName":"3"
}
3.我們要轉(zhuǎn)換為帶下劃線的key,例如把businessLicenseCopy轉(zhuǎn)換為business_license_copy
我們需要修改實(shí)體,加上注解@JSONField
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.annotation.JSONField; import lombok.Data; import java.io.Serializable; ? /** ?* @類名 WeChatBusinessLicenseInfo ?* @描述 營(yíng)業(yè)執(zhí)照/登記證書(shū)信息(測(cè)試用) ?* @版本 1.0 ?* @創(chuàng)建人 XuKang ?* @創(chuàng)建時(shí)間 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.加上注解后打印轉(zhuǎn)換后的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 ?* @描述 營(yíng)業(yè)執(zhí)照/登記證書(shū)信息(測(cè)試用) ?* @版本 1.0 ?* @創(chuàng)建人 XuKang ?* @創(chuàng)建時(shí)間 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.輸出打印轉(zhuǎn)換后的實(shí)體:
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"
}
小結(jié):重命名除@JSONField,還有@JsonProperty、@SerializedName;@JsonProperty主要用于入?yún)⑥D(zhuǎn)換,和Json字符串序列化為Java對(duì)象;@SerializedName 改變了默認(rèn)序列化和默認(rèn)反序列化的字段取值;
@JSONField注解常用的使用場(chǎng)景
應(yīng)用場(chǎng)景:
當(dāng)我們?cè)谂c前端進(jìn)行交互時(shí),前端想要的字段與我們提供的字段名不同,這時(shí)候一種解決方案是修改實(shí)體類,但如果該實(shí)體類應(yīng)用的比較多,那改起來(lái)的代價(jià)太大,因此,可以使用注解@JSONField來(lái)實(shí)現(xiàn)替換效果,用法如下:
@JSONField(name = "size_new") private int size;
一、JSON內(nèi)容與實(shí)體類,@JSONField常規(guī)寫(xiě)法
JSON(與下述JSON字符串內(nèi)容一致)
{ size: 5, weight: 10, colour: "red" }
實(shí)體類(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字符串轉(zhuǎn)對(duì)應(yīng)Java對(duì)象
執(zhí)行代碼
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()); }
運(yùn)行結(jié)果
三、支持序列化和反序列化
源碼中的序列化和反序列化默認(rèn)值均為true,則默認(rèn)情況下是允許該字段序列化和反序列化的,如下:
boolean serialize() default true; boolean deserialize() default true;
使用方法(以下不支持序列化,支持反序列化)
@JSONField(name = "size_new", serialize = false, deserialize = true) private int size;
當(dāng)我們的某些字段為空值時(shí),我們?nèi)韵M麑⒋俗侄畏祷氐角岸耍ㄔ撆渲每梢苑祷貛в锌兆侄蔚淖址?,但是?dāng)字段為基本數(shù)據(jù)類型時(shí)無(wú)效,須將其轉(zhuǎn)換為包裝類)
@JSONField(serialzeFeatures= SerializerFeature.WriteMapNullValue)
四、指定字段順序
將Java對(duì)象轉(zhuǎn)換為JSON格式,轉(zhuǎn)換后的字段順序會(huì)根據(jù)首字母來(lái)排序,亦可通過(guò)如下方式來(lái)指定字段順序:
@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;
執(zhí)行代碼
AppleDO apple = new AppleDO(); apple.setSize(6); apple.setWeight(12); apple.setColour("green"); String appleStr = JSON.toJSONString(apple); System.out.println(appleStr);
加ordinal參數(shù)之前運(yùn)行結(jié)果
加ordinal參數(shù)之后運(yùn)行結(jié)果
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springboot+maven快速構(gòu)建項(xiàng)目的示例代碼
本篇文章主要介紹了springboot+maven快速構(gòu)建項(xiàng)目的示例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08為何修改equals方法時(shí)還要重寫(xiě)hashcode方法的原因分析
這篇文章主要介紹了為何修改equals方法時(shí)還要重寫(xiě)hashcode方法的原因分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Spring boot基于ScheduledFuture實(shí)現(xiàn)定時(shí)任務(wù)
這篇文章主要介紹了Spring boot基于ScheduledFuture實(shí)現(xiàn)定時(shí)任務(wù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06Java 將PPT幻燈片轉(zhuǎn)為HTML文件的實(shí)現(xiàn)思路
本文以Java程序代碼為例展示如何通過(guò)格式轉(zhuǎn)換的方式將PPT幻燈片文檔轉(zhuǎn)為HTML文件,本文通過(guò)實(shí)例代碼圖文相結(jié)合給大家分享實(shí)現(xiàn)思路,需要的朋友參考下吧2021-06-06java:程序包c(diǎn)om.xxx.xxx不存在報(bào)錯(cuò)萬(wàn)能解決辦法
這篇文章主要給大家介紹了關(guān)于java:程序包c(diǎn)om.xxx.xxx不存在報(bào)錯(cuò)萬(wàn)能解決辦法,這個(gè)問(wèn)題曾逼瘋初學(xué)者的我,不過(guò)弄清楚原理后就很簡(jiǎn)單了,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12java執(zhí)行bat命令碰到的阻塞問(wèn)題的解決方法
這篇文章主要介紹了java執(zhí)行bat命令碰到的阻塞問(wèn)題的解決方法,有需要的朋友可以參考一下2014-01-01JavaWeb動(dòng)態(tài)導(dǎo)出Excel可彈出下載
這篇文章主要介紹了JavaWeb動(dòng)態(tài)導(dǎo)出Excel,對(duì)Excel可彈出進(jìn)行下載操作,感興趣的小伙伴們可以參考一下2016-03-03Java?Dubbo服務(wù)調(diào)用擴(kuò)展點(diǎn)Filter使用教程
Dubbo是阿里巴巴公司開(kāi)源的一個(gè)高性能優(yōu)秀的服務(wù)框架,使得應(yīng)用可通過(guò)高性能的RPC實(shí)現(xiàn)服務(wù)的輸出和輸入功能,可以和Spring框架無(wú)縫集成2022-12-12