FastJson只序列化java对象的部分属性
2015-10-12 11:12
627 查看
public class Student { private int id; private String name; private int age; //get set方法略 }
如下方法:
Student student = new Student(0, "Aaron", 24); System.out.println(JSON.toJSONString(student,true));
输出为:
{ "age":24, "id":0, "name":"Aaron" }如果我们想要将实体类中的某个字段或某几个不进行解析呢?那么我们可以使用transient 关键字,来标记它为不需要的,在fastjson中还提供了一种便捷的方法来自定义我们需要序列化的字段,
SimplePropertyPreFilter filter = new SimplePropertyPreFilter(实体类.class, "字段1","字段2"); //字段为我们需要序列化的字段,如果实体类中没有改字段则不解析放弃该字段而不会报错。 SimplePropertyPreFilter filter = new SimplePropertyPreFilter(Student.class, "id","age"); String jsonStu =JSON.toJSONString(students,filter);这样就只会序列化 id和age 的字段。
相关文章推荐
- String类到底可不可变
- java修改JPG图片DPI 不改变分辨率
- SpringMVC的forward和redirect
- 各种Java加密算法二
- 韩顺平 java 第35讲 事件处理机制
- 各种Java加密算法一
- Java从入门到精通06-数组
- Eclipse4.5(Mars)安装SVN插件
- Java基础知识强化之IO流笔记56:IO流练习之 登录注册IO版
- Arrays类的使用
- Java NIO系列教程(七) FileChannel
- java compiler level does not match the version of the installed java project facet错误的解决
- android studio 导入eclipse 项目
- java中基本数据类型的相关问题
- eclipse打签名apk时出现GC limit error
- 【Java】抽象工厂模式
- java 中split()方法使用注意
- 【springmvc+mybatis项目实战】杰信商贸-34.业务出口报运WebService1
- 【SSH三框架】Struts2第六章的基础:他们拦截函数的定义
- Spring Web MVC中的页面缓存支持 ——跟我学SpringMVC系列