fastjson的基本使用方法
来源:互联网 发布:中国网络排行榜 编辑:程序博客网 时间:2024/06/05 16:07
1 fastjson主要的API
fastjson入口类是com.alibaba.fastjson.JSON, 主要API是JSON.toJSONString和parseObject,使用fastjson要注意要转换的类必须有默认的无参构造方法。
序列化:
String jsonString = JSON.toJSONString(obj);
反序列化:
VO vo = JSON.parseObject("jsonString", VO.class);
泛型反序列化:
List<VO> list = JSON.parseObject("jsonString", new TypeReference<List<VO>>(){});注意:VO必须有默认无参构造方法
- 1
- 2
- 3
- 1
- 2
- 3
- 其他主要API:
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合 public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本 public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
说明:
SerializeWriter:相当于StringBuffer
JSONArray:相当于List
JSONObject:相当于Map
JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd"; JSON.toJSONString(obj, SerializerFeature.WriteDateUseDateFormat);
- 1
- 2
- 1
- 2
反序列化能够自动识别如下日期格式:
- ISO-8601日期格式
- yyyy-MM-dd
- yyyy-MM-dd HH:mm:ss
- yyyy-MM-dd HH:mm:ss.SSS
- 毫秒数字
- 毫秒数字字符串
- NET JSON日期格式
- new Date(198293238)
3 过滤属性
需要根据不同的环境返回定制化返回属性时,可以使用SimplePropertyPreFilter。
- SimplePropertyPreFilter的代码接口如下:
public class SimplePropertyPreFilter implements PropertyPreFilter { public SimplePropertyPreFilter(String... properties){ this(null, properties); } public SimplePropertyPreFilter(Class<?> clazz, String... properties){ ... ... } public Class<?> getClazz() { return clazz; } public Set<String> getIncludes(); public Set<String> getExcludes(); ... }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
你可以配置includes、excludes。当class不为null时,针对特定类型;当class为null时,针对所有类型。
当includes的size > 0时,属性必须在includes中才会被序列化,excludes优先于includes。
- 使用方法:
在1.1.23版本之后,JSON提供新的序列化接口toJSONString,如下:
String JSON.toJSONString(Object, SerializeFilter, SerializerFeature...);
用法如下:
User user = new User(); user.setName("shamo"); user.setPwd("123"); user.setAge(25); SimplePropertyPreFilter filter = new SimplePropertyPreFilter(User.class, "pwd", "age"); Set<String> includes = filter.getIncludes(); Set<String> excludes = filter.getExcludes(); includes.add("name"); excludes.add("pwd"); String json = JSON.toJSONString(user, filter); System.out.println(json);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
输出:
{"age":25,"name":"shamo"}
4 修改属性
可以使用JSONObject对json形式的字符串进行任意的增删改
用法如下:
String jsonString = JSON.toJSONString(user);JSONObject jsonObject = JSON.parseObject(jsonString);jsonObject.put("gender", "male");jsonObject.put("pwd","456");jsonObject.remove("name");String json = JSON.toJSONString(jsonObject);System.out.println(json);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 1
- 2
- 3
- 4
- 5
- 6
- 7
输出:
{"pwd":"456","age":25,"gender":"male"}
- 1
- 1
5 SerializerFeature序列化属性
DisableCheckSpecialChar:一个对象的字符串属性中如果有特殊字符如双引号,将会在转成json时带有反斜杠转移符。如果不需要转义,可以使用这个属性。默认为false QuoteFieldNames———-输出key时是否使用双引号,默认为true WriteMapNullValue——–是否输出值为null的字段,默认为false WriteNullNumberAsZero—-数值字段如果为null,输出为0,而非null WriteNullListAsEmpty—–List字段如果为null,输出为[],而非null WriteNullStringAsEmpty—字符类型字段如果为null,输出为”“,而非null WriteNullBooleanAsFalse–Boolean字段如果为null,输出为false,而非nullWriteClassName:支持序列化时写入类型信息,从而使得反序列化时不丢失类型信息。例如:Color color = Color.RED; String text = JSON.toJSONString(color,SerializerFeature.WriteClassName); System.out.println(text); 输出结果:{"@type":"java.awt.Color","r":255,"g":0,"b":0,"alpha":255}由于序列化带了类型信息,使得反序列化时能够自动进行类型识别,例如:String text = ...; //{"@type":"java.awt.Color","r":255,"g":0,"b":0,"alpha":255}Color color = (Color) JSON.parse(text);
阅读全文
0 0
- fastjson的基本使用方法
- fastjson的基本使用方法
- fastjson的基本使用方法
- JSON-FastJson的基本使用方法
- fastjson 基本使用方法
- fastjson的常用使用方法
- FastJson的使用方法总结
- FastJson的使用方法总结
- FastJson的基本使用
- FastJson的基本使用
- FastJson的基本使用
- FastJson的基本使用
- fastJson的基本使用
- FastJson的基本使用
- FastJson的基本使用
- Fastjson的基本使用
- FastJson的基本使用
- FastJson的基本使用
- 设计模式之观察者模式
- 特殊的Daemon线程
- 元素等待————显式等待二
- 数字电路时序分析(二)
- 排序算法
- fastjson的基本使用方法
- 强化学习简明教程
- 数据结构之哈夫曼树
- 使用ElasticSearch完成百万级数据查询附近的人功能
- 【自用】 javanote170727(类与对象、构造方法、修饰符、变量的作用域)
- HDU 6040 Hints of sd0061 (技巧)
- Unity3D之Mecanim动画系统学习笔记(五):Animator Controller
- codeforces 118B Present from Lena
- Scrapy 安装selenium时PhantomJS不可用解决