您好,登录后才能下订单哦!
本篇内容介绍了“Java中@JSONField对象字段重命名和顺序问题怎么解决”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
<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参数之后运行结果
“Java中@JSONField对象字段重命名和顺序问题怎么解决”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。