指定JSONString中实体类属性的输出顺序JSONString过程中出现实体类的属性与转换之前的顺序不⼀致
public static void main(String[] args){
Person person =new Person();
person.setName("Tom");
c++string类型person.setAge(20);
person.setGender("Male");
person.setHeight(180.5);
person.setWeight(80.5);
System.out.JSONString(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;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。