FastJson bean序列化属性顺序问题
fastjson
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
项目地址:https://gitcode.com/gh_mirrors/fastj/fastjson
·
fastjson序列化一个java bean,默认是根据fieldName的字母序进行序列化的,你可以通过ordinal指定字段的顺序,这个特性需要1.1.42以上版本。示例如下。
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
/**
* Created by gary on 16/6/21.
*/
public class FieldSequence {
@JSONField(ordinal=1,name = "name_1")
private String name;
@JSONField(ordinal=2)
private int age;
@JSONField(ordinal=3)
private String gender;
public FieldSequence(String name,int age,String gender){
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public static void main(String[] args){
FieldSequence fieldSequence = new FieldSequence("lily",20,"woman");
System.out.println(JSON.toJSONString(fieldSequence));
}
}
属性中不使用@JSONField注解输出如下
{"age":20,"gender":"woman","name":"lily"} //按属性字母顺序排序
使用注解后,上面程序输出如下
{"name_1":"lily","age":20,"gender":"woman"}
@JSONField注解中可以设置属性顺序,重新设置属性名称,格式等,SerializerFeature(这是个枚举类,里面封装的有很多序列化的格式需求)
FASTJSON 2.0.x has been released, faster and more secure, recommend you upgrade.
最近提交(Master分支:1 个月前 )
c942c834 - 2 年前
5bc4709b - 2 年前
新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。
更多推荐


所有评论(0)