需求及背景:
需求/要求是五花八门,时有超出预期;今儿遇到了要求根据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 Fast json BeanSort();
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));
}
}