• 指定JSON.toJSONString中实体类属性的输出顺序


    最近在使用JSON.toJSONString过程中出现实体类的属性与转换之前的顺序不一致
    public static void main(String[] args) {
            Person person = new Person();
            person.setName("Tom");
            person.setAge(20);
            person.setGender("Male");
            person.setHeight(180.5);
            person.setWeight(80.5);
            
            System.out.println(JSON.toJSONString(person));
        }
    使用注解指定属性的输出顺序

    第一种:通过在实体类添加
    @JSONType(orders={"name","age","gender","height","weight"})
    @JSONType(orders={"name","age","gender","height","weight"})
    public class Person {
        private String name;
        private Integer age;
        private String gender;
        private Double height;
        private Double weight;
    }
    第二种:在属性上添加@JSONField(ordinal = 1)
    public class Person {
        @JSONField(ordinal = 1)
        private String name;
        @JSONField(ordinal = 2)
        private Integer age;
        @JSONField(ordinal = 3)
        private String gender;
        @JSONField(ordinal = 4)
        private Double height;
        @JSONField(ordinal = 5)
        private Double weight;
    }
     
  • 相关阅读:
    CSP2020 游记
    关于对拍
    联考day7
    CSP 模拟26
    动态规划
    JAVA单元测试的用法和要点(入门篇)
    乐观锁和悲观锁的区别
    并发基础篇(六):线程Thread类的start()方法和run()方法【转载】
    this和super差异
    Java中的static关键字解析
  • 原文地址:https://www.cnblogs.com/kuoAT/p/7088657.html
Copyright © 2020-2023  润新知