一、引入maven依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.66</version>
</dependency>
二、字段重命名
1.創(chuàng)建一個測試實體
import lombok.Data;
import java.io.Serializable;
/**
* @類名 WeChatBusinessLicenseInfo
* @描述 營業(yè)執(zhí)照/登記證書信息(測試用)
* @版本 1.0
* @創(chuàng)建人 XuKang
* @創(chuàng)建時間 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.將實體轉(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
我們需要修改實體,加上注解@JSONField
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
import java.io.Serializable;
/**
* @類名 WeChatBusinessLicenseInfo
* @描述 營業(yè)執(zhí)照/登記證書信息(測試用)
* @版本 1.0
* @創(chuàng)建人 XuKang
* @創(chuàng)建時間 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è)執(zhí)照/登記證書信息(測試用)
* @版本 1.0
* @創(chuàng)建人 XuKang
* @創(chuàng)建時間 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)換后的實體:
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對象;文章來源:http://www.zghlxwxcb.cn/news/detail-402204.html
@SerializedName 改變了默認(rèn)序列化和默認(rèn)反序列化的字段取值;文章來源地址http://www.zghlxwxcb.cn/news/detail-402204.html
到了這里,關(guān)于Java 對象轉(zhuǎn)Json,@JSONField對象字段重命名和順序問題的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!