标签:开源中国 object china 处理程序 string sea tde content 包括
fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。
Fastjson已经被广泛使用在各种场景,包括cache存储、RPC通讯、MQ通讯、网络协议通讯、Android客户端、Ajax服务器处理程序等等。
fastjson相对其他JSON库的特点是快,从2011年fastjson发布1.1.x版本之后,其性能从未被其他Java实现的JSON库超越。
fastjson在阿里巴巴大规模使用,在数万台服务器上部署,fastjson在业界被广泛接受。在2012年被开源中国评选为最受欢迎的国产开源软件之一。
fastjson有非常多的testcase,在1.2.11版本中,testcase超过3321个。每次发布都会进行回归测试,保证质量稳定。
fastjson的API十分简洁。
String text = JSON.toJSONString(obj); //序列化
VO vo = JSON.parseObject("{...}", VO.class); //反序列化
支持泛型,支持流处理超大文本,支持枚举,支持序列化和反序列化扩展。
以下例子对JSON.toJSONString和JSON.parseObject这2个重要方法进行测试:
Fruit实体类:
1 package com.led.daorumysql; 2 3 /** 4 * 定义水果实体类 5 * @author 86157 6 * 7 */ 8 public class Fruit { 9 10 //水果名字 11 private String name; 12 //水果产地 13 private String origin; 14 //水果价格 15 private Integer price; 16 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public String getOrigin() { 24 return origin; 25 } 26 public void setOrigin(String origin) { 27 this.origin = origin; 28 } 29 public Integer getPrice() { 30 return price; 31 } 32 public void setPrice(Integer price) { 33 this.price = price; 34 } 35 @Override 36 public String toString() { 37 return "Fruit [name=" + name + ", origin=" + origin + ", price=" + price + "]"; 38 } 39 40 public Fruit(String name, String origin, Integer price) { 41 super(); 42 this.name = name; 43 this.origin = origin; 44 this.price = price; 45 } 46 47 public Fruit() { 48 super(); 49 } 50 51 52 53 54 }
主类:
1 package com.led.daorumysql; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 6 import com.alibaba.fastjson.JSON; 7 8 /** 9 * FastJSON测试类 10 * @author 86157 11 * 12 */ 13 public class FastJSONTest { 14 15 public static void main(String[] args) { 16 //实例化Fruit类 17 Fruit fruitOne = new Fruit("apple", "America", 13); 18 Fruit fruitTwo = new Fruit("orange", "China", 15); 19 //创建fruitList对象 20 List<Fruit> fruitList = new ArrayList<Fruit>(); 21 fruitList.add(fruitOne); 22 fruitList.add(fruitTwo); 23 24 //将java对象(Fruit)转成json字符串 25 System.out.println("=========将java对象(Fruit)转成json字符串==========="); 26 String fruitOne_jsonString = JSON.toJSONString(fruitOne); 27 System.out.println("fruitOne_jsonString: " + fruitOne_jsonString); 28 System.out.println(); 29 30 //将json字符串转成java对象(Fruit) 31 System.out.println("=========将json字符串转成java对象(Fruit)==========="); 32 Fruit fruitOne_parseObject = JSON.parseObject(fruitOne_jsonString, Fruit.class); 33 System.out.println("fruit object: " + fruitOne_parseObject); 34 System.out.println("fruit name: " + fruitOne_parseObject.getName()); 35 System.out.println("fruit origin: " + fruitOne_parseObject.getOrigin()); 36 System.out.println("fruit price: " + fruitOne_parseObject.getPrice()); 37 System.out.println(); 38 39 //将java对象(fruitList)转成json字符串 40 System.out.println("=========将java对象(fruitList)转成json字符串========"); 41 String fruitList_jsonString = JSON.toJSONString(fruitList); 42 System.out.println("fruitList_jsonString: " + fruitList_jsonString); 43 System.out.println(); 44 45 //将json字符串转成java对象(fruitList) 46 System.out.println("=========将json字符串转成java对象(fruitList)========"); 47 List<Fruit> parseArray = JSON.parseArray(fruitList_jsonString, Fruit.class); 48 System.out.println("fruitOne object: " + parseArray.get(0).toString()); 49 System.out.println("fruitTwo object: " + parseArray.get(1).toString()); 50 System.out.println("fruitTwo object name: " + parseArray.get(1).getName()); 51 52 } 53 }
控制台输出:
1 =========将java对象(Fruit)转成json字符串=========== 2 fruitOne_jsonString: {"name":"apple","origin":"America","price":13} 3 4 =========将json字符串转成java对象(Fruit)=========== 5 fruit object: Fruit [name=apple, origin=America, price=13] 6 fruit name: apple 7 fruit origin: America 8 fruit price: 13 9 10 =========将java对象(fruitList)转成json字符串======== 11 fruitList_jsonString: [{"name":"apple","origin":"America","price":13},{"name":"orange","origin":"China","price":15}] 12 13 =========将json字符串转成java对象(fruitList)======== 14 fruitOne object: Fruit [name=apple, origin=America, price=13] 15 fruitTwo object: Fruit [name=orange, origin=China, price=15] 16 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)
标签:开源中国 object china 处理程序 string sea tde content 包括
原文地址:https://www.cnblogs.com/stm32stm32/p/9001715.html