网站首页 > 开源技术 正文
需求及背景:
需求/要求是五花八门,时有超出预期;今儿遇到了要求根据JSON属性名排序问题,工程用的FastJson,于是研究了一下,做个记录。
FastJson格式化成JSON字符串,默认输出的排序方式是根据属性首字母【a-z】排序的,如果首字母一样,就根据第二个字母,以此类推;
FastJson格式化,如要按照指定的顺序排序输出,有2种方式可实现:1、Bean的属性注解:@JSONField(ordinal = 2) ,2、Bean类注解:@JSONType() ,如下是代码验证。
1、使用:@JSONField(ordinal = 2) 注解属性指定排序
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import lombok.Data;
@Data
public class FastjsonBeanSort {
@JSONField(ordinal = 4)
private int f1;
@JSONField(ordinal = 3)
private int f2;
@JSONField(ordinal = 2)
private int f3;
@JSONField(ordinal = 0)
private String n1;
private String s111;
@JSONField(name = "s222", ordinal = 1)
private String s2;
@JSONField(name = "s333", ordinal = 0)
private String s3;
public static void main(String[] args) {
// 使用ordinal指定字段的顺序
FastjsonBeanSort fastjsonBeanSort = new FastjsonBeanSort();
fastjsonBeanSort.setF1(1111);
fastjsonBeanSort.setF2(2222);
fastjsonBeanSort.setF2(3333);
fastjsonBeanSort.setN1("n1");
fastjsonBeanSort.setS111("1111");
fastjsonBeanSort.setS2("2222");
fastjsonBeanSort.setS3("3333");
System.out.println(JSON.toJSONString(fastjsonBeanSort));
}
}
2、使用:@JSONType()注解Bean类指定排序
import com.alibaba.fastjson.JSON;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
public class JSONSortedBean {
private String a;
private String ac;
private String ab;
private String d;
private List<JSONSortedBean> sortedBeanList;
public static void main(String[] args) {
JSONSortedBean parentJSONSortedBean = new JSONSortedBean();
parentJSONSortedBean.setA("aaaa");
parentJSONSortedBean.setD("dddd");
parentJSONSortedBean.setAc("acac");
parentJSONSortedBean.setAb("abab");
List<JSONSortedBean> sortedBeanList = new ArrayList<>();
JSONSortedBean jsonSortedBean = new JSONSortedBean();
jsonSortedBean.setA("a7234");
jsonSortedBean.setD("dddd");
jsonSortedBean.setAc("acac");
jsonSortedBean.setAb("abab");
sortedBeanList.add(jsonSortedBean);
JSONSortedBean jsonSortedBean2 = new JSONSortedBean();
jsonSortedBean2.setA("a1234");
jsonSortedBean2.setD("dddd");
jsonSortedBean2.setAc("acac");
jsonSortedBean2.setAb("abab");
sortedBeanList.add(jsonSortedBean2);
JSONSortedBean jsonSortedBean3 = new JSONSortedBean();
jsonSortedBean3.setA("a5234");
jsonSortedBean3.setD("dddd");
jsonSortedBean3.setAc("acac");
jsonSortedBean3.setAb("abab");
sortedBeanList.add(jsonSortedBean3);
parentJSONSortedBean.setSortedBeanList(sortedBeanList);
// 默认: json串按字母排序
System.out.println(JSON.toJSONString(parentJSONSortedBean));
}
}
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONType;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
@Data
@JSONType(orders = {"d", "ab", "ac", "a"})
public class JSONSortedBean2 {
private String a;
private String ac;
private String ab;
private String d;
private List<JSONSortedBean2> sortedBeanList;
public static void main(String[] args) {
JSONSortedBean2 parentJSONSortedBean = new JSONSortedBean2();
parentJSONSortedBean.setA("aaaa");
parentJSONSortedBean.setD("dddd");
parentJSONSortedBean.setAc("acac");
parentJSONSortedBean.setAb("abab");
List<JSONSortedBean2> sortedBeanList = new ArrayList<>();
JSONSortedBean2 jsonSortedBean = new JSONSortedBean2();
jsonSortedBean.setA("a7234");
jsonSortedBean.setD("dddd");
jsonSortedBean.setAc("acac");
jsonSortedBean.setAb("abab");
sortedBeanList.add(jsonSortedBean);
JSONSortedBean2 jsonSortedBean2 = new JSONSortedBean2();
jsonSortedBean2.setA("a1234");
jsonSortedBean2.setD("dddd");
jsonSortedBean2.setAc("acac");
jsonSortedBean2.setAb("abab");
sortedBeanList.add(jsonSortedBean2);
JSONSortedBean2 jsonSortedBean3 = new JSONSortedBean2();
jsonSortedBean3.setA("a5234");
jsonSortedBean3.setD("dddd");
jsonSortedBean3.setAc("acac");
jsonSortedBean3.setAb("abab");
sortedBeanList.add(jsonSortedBean3);
parentJSONSortedBean.setSortedBeanList(sortedBeanList);
// 默认: json串按字母排序
System.out.println(JSON.toJSONString(parentJSONSortedBean));
}
}
猜你喜欢
- 2025-01-31 RETE算法简述 & 实践(reptile算法)
- 2025-01-31 面试必备:30 个 Java 集合面试问题及答案
- 2025-01-31 Java关于Exception和Error以及处理机制解析
- 2025-01-31 java-常用加解密算法-Md5+salt及具体使用
- 2025-01-31 100个Java工具类之6:用4种方式发起HTTP请求
- 2025-01-31 8种开发工具,拒绝加班熬夜(开发工具是怎么做的)
- 2025-01-31 Java实现调用HTTP请求的几种常见方式
- 2025-01-31 SpringBoot中使用LocalDateTime踩坑记录
- 2025-01-31 公司大佬对 Excel 导入、导出的封装,那叫一个妙啊!
- 2025-01-31 SpringBoot集成FastDFS案例分享(springboot集成flowable modeler)
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)