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 改变了默认序列化和默认反序列化的字段取值;
Java 对象转Json,@JSONField对象字段重命名和顺序问题相关推荐
- JOOQ学习笔记:分页、排序、字段重命名的写法
环境 JOOQ: "3.9.6" java:1.8 springboot:1.5.10.RELEASE 前言 最近进小黑屋赶项目: 公司封装的分页方法,字段接收上,不能满足我,所以 ...
- c#字符串转为json对象与json转对象
using System.Collections.Generic; using Newtonsoft.Json.Linq; using Newtonsoft.Json; using System.We ...
- layui中的table使用心得,json格式处理,json转字符串,json转对象,json转对象数组,layui表格图片显示修改。
文章目录 案例一.layui中table,后台数据是json格式的处理 1. 前端显示样式 2. 前端代码 3. 后端代码:layui前端表格需要返回的数据格式要求示例 4. json格式处理工具类: ...
- python 对象转换为json,Python对象转换为json的方法步骤
python中内置了json库,用起来超级方便,json现在以成为开发的必备. python对象到json字符串的转换规则: python json dict object list, tuple a ...
- mysql字段重命名_MySQL中使用SQL语句对字段进行重命名
MySQL中,如何使用SQL语句来对表中某一个字段进行重命名呢?我们将使用alter table 这一SQL语句. 重命名字段的语法为:alter table change . 现在我们来尝试把tes ...
- oracle 字段重命名大表,Oracle表字段的增、刪、改、表的重命名及主鍵的增、刪、改...
一.表字段的增刪改: 添加字段的語法:alter table tablename add (column datatype [default value][null/not null],-.); 修改 ...
- 如何将图片一键重命名按顺序_如何“智慧”地管理文件?(一次付出,终身受益)...
1.阅读本文预计需要 8 分钟,并提供了资源下载. 2.本文是「电脑爱好者」专题C15篇的首次发布. 3.第一时间获取更新,欢迎关注「奔跑中的奶酪」. 导读 网上很少有对海量文件管理进行理论和实践说明 ...
- 如何将图片一键重命名按顺序_图片批量重命名工具(RenameIt)
其主要功能是将某个目录下的指定文件复制到另一个目录下,同时: 1. 对于目录结构,可以选择将原各级子目录合并成一个目录,或保持原目录结构. 2. 对于文件名,可以选择(1).保持原文件名不变,但在重名 ...
- 如何将图片一键重命名按顺序_再发一个多目录下图片按创建时间批量重命名的code...
在筛选比较好的照片时中间排序出现断档,结果手贱重命名时候把图片时序搞乱了,直接GG.还好写了个code拯救了一下.总的来说就是把命名乱掉的图片先按照系统时间戳进行升序排列,再根据相应顺序进行重命名. ...
最新文章
- 【Python学习笔记】输入raw_input(),特殊情况下也可以用input()
- C++迟后联编和虚函数表
- Android rxjava2的disposable
- 第一个 Win32 窗口程序
- Spring Transaction 使用入门 (转)
- linux 查看磁盘空间大小命令
- CStatic控件的基本使用
- 【CodeForces】Round #436
- 深度学习----一文搞懂激活函数(Sigmoid/ReLU/LeakyReLU/PReLU/ELU)
- python实现自动打电话软件_python拨打电话
- windows自带黑体_win10字体设置黑体|win10系统文字如何设置黑体字体
- 985研究生被小公司领导留了3年,【全栈最全Java框架总结
- 台式计算机如何连接手机热点上网,家里台式没有网络 想用手机热点来连接电脑上网 怎么处理?...
- 奇怪的同床人:指纹现象……或state.gov与facebook.com
- [问题已处理]-centos7 history命令没有任何记录
- CocosCreator之KUOKUO趣味文章:小怪要绕墙 3
- Java输出九九乘法表(99乘法)
- 推荐算法之AFM模型(注意力机制的引入)
- uIP 一个免费的TCP/IP栈
- iOS开发之开源项目库总汇