<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
<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引數之後執行結果
以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。
相關文章
<em>Mac</em>Book项目 2009年学校开始实施<em>Mac</em>Book项目,所有师生配备一本<em>Mac</em>Book,并同步更新了校园无线网络。学校每周进行电脑技术更新,每月发送技术支持资料,极大改变了教学及学习方式。因此2011
2021-06-01 09:32:01
综合看Anker超能充系列的性价比很高,并且与不仅和iPhone12/苹果<em>Mac</em>Book很配,而且适合多设备充电需求的日常使用或差旅场景,不管是安卓还是Switch同样也能用得上它,希望这次分享能给准备购入充电器的小伙伴们有所
2021-06-01 09:31:42
除了L4WUDU与吴亦凡已经多次共事,成为了明面上的厂牌成员,吴亦凡还曾带领20XXCLUB全队参加2020年的一场音乐节,这也是20XXCLUB首次全员合照,王嗣尧Turbo、陈彦希Regi、<em>Mac</em> Ova Seas、林渝植等人全部出场。然而让
2021-06-01 09:31:34
目前应用IPFS的机构:1 谷歌<em>浏览器</em>支持IPFS分布式协议 2 万维网 (历史档案博物馆)数据库 3 火狐<em>浏览器</em>支持 IPFS分布式协议 4 EOS 等数字货币数据存储 5 美国国会图书馆,历史资料永久保存在 IPFS 6 加
2021-06-01 09:31:24
开拓者的车机是兼容苹果和<em>安卓</em>,虽然我不怎么用,但确实兼顾了我家人的很多需求:副驾的门板还配有解锁开关,有的时候老婆开车,下车的时候偶尔会忘记解锁,我在副驾驶可以自己开门:第二排设计很好,不仅配置了一个很大的
2021-06-01 09:30:48
不仅是<em>安卓</em>手机,苹果手机的降价力度也是前所未有了,iPhone12也“跳水价”了,发布价是6799元,如今已经跌至5308元,降价幅度超过1400元,最新定价确认了。iPhone12是苹果首款5G手机,同时也是全球首款5nm芯片的智能机,它
2021-06-01 09:30:45