By
Macintosh-c
总阅读 次
@JSONField 注解说明
导入@JSONField 注解:
import com.alibaba.fastjson.annotation.JSONField;
常见用法:
1. 指定字段的名称
1 2
| @JSONField(name="role_name") private String roleName;
|
1 2 3 4 5
| public class A { // 配置date序列化和反序列使用yyyyMMdd日期格式 @JSONField(format="yyyyMMdd") public Date date; }
|
3. 指定字段的顺序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| 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"}
4. 使用serialize/deserialize指定字段不序列化
1 2 3 4
| public class A { @JSONField(serialize=false) public Date date; }
|
------------------------------------------------------------------------------------------------------------------------------
打赏我的人,运气都不会太差哦~
------------------------------------------------------------------------------------------------------------------------------
打赏我的人,运气都不会太差哦~