Java对象转Json,@JSONField对象字段重命名和顺序
一、引入maven依赖
二、字段重命名
三、字段排序
@JSONField注解常用的使用场景
一、JSON内容与实体类,@JSONField常规写法
二、JSON字符串转对应Java对象
三、支持序列化和反序列化
四、指定字段顺序
Java对象转Json,@JSONField对象字段重命名和顺序 一、引入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对象;@SerializedName 改变了默认序列化和默认反序列化的字段取值;
@JSONField注解常用的使用场景应用场景:
当我们在与前端进行交互时,前端想要的字段与我们提供的字段名不同,这时候一种解决方案是修改实体类,但如果该实体类应用的比较多,那改起来的代价太大,因此,可以使用注解@JSONField来实现替换效果,用法如下:
@JSONField(name = "size_new")
private int size;
一、JSON内容与实体类,@JSONField常规写法
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;
}
}
二、JSON字符串转对应Java对象
执行代码
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参数之后运行结果
以上为个人经验,希望能给大家一个参考,也希望大家多多支持软件开发网。