一、引入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",
"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对象;文章来源:https://www.toymoban.com/news/detail-402204.html
@SerializedName 改变了默认序列化和默认反序列化的字段取值;文章来源地址https://www.toymoban.com/news/detail-402204.html
到了这里,关于Java 对象转Json,@JSONField对象字段重命名和顺序问题的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!