@JSONField的使用
Posted 横看成岭侧成峰-28
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了@JSONField的使用相关的知识,希望对你有一定的参考价值。
fastjson支持多种方式定制序列化:
- 通过@JSONField定制序列化
- 通过@JSONType定制序列化
- 通过SerializeFilter定制序列化
- 通过ParseProcess定制反序列化
@JSONField的使用:
1. 利用@JSONField的format配置日期格式化,这个在上一篇文章有所展示。
2. 利用@JSONField的serialize指定字段不序列化:
package com.ant.jdk8.json; import com.alibaba.fastjson.annotation.JSONField; import java.util.Date; public class User { private String name; @JSONField(serialize=false) private int age; @JSONField(format="yyyy-MM-dd HH:mm:ss") private Date hireDate; 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 Date getHireDate() { return hireDate; } public void setHireDate(Date hireDate) { this.hireDate = hireDate; } public User(String name, int age, Date hireDate) { this.name = name; this.age = age; this.hireDate = hireDate; } public User() { } }
package com.ant.jdk8.json; import com.alibaba.fastjson.JSON; import java.util.ArrayList; import java.util.Date; import java.util.List; public class JsonDemo { public static void main(String[] args) { convertCollectionToJson(); } public static void convertCollectionToJson(){ List<User> userList = new ArrayList<>(); userList.add(new User("tom",20,new Date())); userList.add(new User("jack",25,new Date())); String jsonString = JSON.toJSONString(userList); System.out.println(jsonString); } }
3. 利用@JSONField的ordinal指定序列化的顺序(这个特性需要1.1.42以上版本):
package com.ant.jdk8.json; import com.alibaba.fastjson.annotation.JSONField; import java.util.Date; public class User { @JSONField(ordinal=3) private String name; @JSONField(ordinal=2) private int age; @JSONField(format="yyyy-MM-dd HH:mm:ss",ordinal = 1) private Date hireDate; 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 Date getHireDate() { return hireDate; } public void setHireDate(Date hireDate) { this.hireDate = hireDate; } public User(String name, int age, Date hireDate) { this.name = name; this.age = age; this.hireDate = hireDate; } public User() { } }
package com.ant.jdk8.json; import com.alibaba.fastjson.JSON; import java.util.ArrayList; import java.util.Date; import java.util.List; public class JsonDemo { public static void main(String[] args) { convertCollectionToJson(); } public static void convertCollectionToJson(){ List<User> userList = new ArrayList<>(); userList.add(new User("tom",20,new Date())); userList.add(new User("jack",25,new Date())); String jsonString = JSON.toJSONString(userList); System.out.println(jsonString); } }
4. 利用@JSONField的name指定字段的名称:
package com.ant.jdk8.json; import com.alibaba.fastjson.annotation.JSONField; import java.util.Date; public class User { @JSONField(ordinal=3,name="username") private String name; @JSONField(ordinal=2) private int age; @JSONField(format="yyyy-MM-dd HH:mm:ss",ordinal = 1) private Date hireDate; 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 Date getHireDate() { return hireDate; } public void setHireDate(Date hireDate) { this.hireDate = hireDate; } public User(String name, int age, Date hireDate) { this.name = name; this.age = age; this.hireDate = hireDate; } public User() { } }
package com.ant.jdk8.json; import com.alibaba.fastjson.JSON; import java.util.ArrayList; import java.util.Date; import java.util.List; public class JsonDemo { public static void main(String[] args) { convertCollectionToJson(); } public static void convertCollectionToJson(){ List<User> userList = new ArrayList<>(); userList.add(new User("tom",20,new Date())); userList.add(new User("jack",25,new Date())); String jsonString = JSON.toJSONString(userList); System.out.println(jsonString); } }
以上是关于@JSONField的使用的主要内容,如果未能解决你的问题,请参考以下文章
我应该如何从 bradjasper 的 django-jsonfield 升级到 Django 的内置 jsonfield?
fastjson简单使用demo,@JSONField注解属性字段上与setget方法上。实体类toString(),实体类转json的区别;_下划线识别