1. 什么是fastjson?
fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。
Fastjson使用场景
Fastjson已经被广泛使用在各种场景,包括cache存储、RPC通讯、MQ通讯、网络协议通讯、Android客户端、Ajax服务器处理程序等等。
2.fastjson的优点
2.1 速度快
fastjson相对其他JSON库的特点是快,从2011年fastjson发布1.1.x版本之后,其性能从未被其他Java实现的JSON库超越。
2.2 使用广泛
fastjson在阿里巴巴大规模使用,在数万台服务器上部署,fastjson在业界被广泛接受。在2012年被开源中国评选为最受欢迎的国产开源软件之一。
2.3 测试完备
fastjson有非常多的testcase,在1.2.11版本中,testcase超过3321个。每次发布都会进行回归测试,保证质量稳定。
2.4 使用简单
fastjson的API十分简洁。
String text = JSON.toJSONString(obj); //序列化 VO vo = JSON.parseObject("{...}", VO.class); //反序列化
2.5 功能完备
支持泛型,支持流处理超大文本,支持枚举,支持序列化和反序列化扩展。
以下例子对JSON.toJSONString和JSON.parseObject这2个重要方法进行测试:
Fruit实体类:
package com.led.daorumysql; /** * 定义水果实体类 * @author 86157 * */ public class Fruit { //水果名字 private String name; //水果产地 private String origin; //水果价格 private Integer price; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getOrigin() { return origin; } public void setOrigin(String origin) { this.origin = origin; } public Integer getPrice() { return price; } public void setPrice(Integer price) { this.price = price; } @Override public String toString() { return "Fruit [name=" + name + ", origin=" + origin + ", price=" + price + "]"; } public Fruit(String name, String origin, Integer price) { super(); this.name = name; this.origin = origin; this.price = price; } public Fruit() { super(); } }
主类:
package com.led.daorumysql; import java.util.ArrayList; import java.util.List; import com.alibaba.fastjson.JSON; /** * FastJSON测试类 * @author 86157 * */ public class FastJSONTest { public static void main(String[] args) { //实例化Fruit类 Fruit fruitOne = new Fruit("apple", "America", 13); Fruit fruitTwo = new Fruit("orange", "China", 15); //创建fruitList对象 List<Fruit> fruitList = new ArrayList<Fruit>(); fruitList.add(fruitOne); fruitList.add(fruitTwo); //将java对象(Fruit)转成json字符串 System.out.println("=========将java对象(Fruit)转成json字符串==========="); String fruitOne_jsonString = JSON.toJSONString(fruitOne); System.out.println("fruitOne_jsonString: " + fruitOne_jsonString); System.out.println(); //将json字符串转成java对象(Fruit) System.out.println("=========将json字符串转成java对象(Fruit)==========="); Fruit fruitOne_parseObject = JSON.parseObject(fruitOne_jsonString, Fruit.class); System.out.println("fruit object: " + fruitOne_parseObject); System.out.println("fruit name: " + fruitOne_parseObject.getName()); System.out.println("fruit origin: " + fruitOne_parseObject.getOrigin()); System.out.println("fruit price: " + fruitOne_parseObject.getPrice()); System.out.println(); //将java对象(fruitList)转成json字符串 System.out.println("=========将java对象(fruitList)转成json字符串========"); String fruitList_jsonString = JSON.toJSONString(fruitList); System.out.println("fruitList_jsonString: " + fruitList_jsonString); System.out.println(); //将json字符串转成java对象(fruitList) System.out.println("=========将json字符串转成java对象(fruitList)========"); List<Fruit> parseArray = JSON.parseArray(fruitList_jsonString, Fruit.class); System.out.println("fruitOne object: " + parseArray.get(0).toString()); System.out.println("fruitTwo object: " + parseArray.get(1).toString()); System.out.println("fruitTwo object name: " + parseArray.get(1).getName()); } }
控制台输出:
=========将java对象(Fruit)转成json字符串=========== fruitOne_jsonString: {"name":"apple","origin":"America","price":13} =========将json字符串转成java对象(Fruit)=========== fruit object: Fruit [name=apple, origin=America, price=13] fruit name: apple fruit origin: America fruit price: 13 =========将java对象(fruitList)转成json字符串======== fruitList_jsonString: [{"name":"apple","origin":"America","price":13},{"name":"orange","origin":"China","price":15}] =========将json字符串转成java对象(fruitList)======== fruitOne object: Fruit [name=apple, origin=America, price=13] fruitTwo object: Fruit [name=orange, origin=China, price=15] fruitTwo object name: orange
注意:
实体类中要创建构造方法,不然parseObject会报如下错误:
Exception in thread "main" com.alibaba.fastjson.JSONException: default constructor not found. class com.led.daorumysql.Fruit at com.alibaba.fastjson.util.JavaBeanInfo.build(JavaBeanInfo.java:197) at com.alibaba.fastjson.parser.ParserConfig.createJavaBeanDeserializer(ParserConfig.java:465) at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:412) at com.alibaba.fastjson.parser.ParserConfig.getDeserializer(ParserConfig.java:324) at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:594) at com.alibaba.fastjson.parser.DefaultJSONParser.parseObject(DefaultJSONParser.java:569) at com.alibaba.fastjson.JSON.parseObject(JSON.java:257) at com.alibaba.fastjson.JSON.parseObject(JSON.java:227) at com.alibaba.fastjson.JSON.parseObject(JSON.java:186) at com.alibaba.fastjson.JSON.parseObject(JSON.java:310) at com.led.daorumysql.FastJSONTest.main(FastJSONTest.java:27)