阿里巴巴FastJSON使用實例

 

1. 什麼是fastjson?

fastjson是阿里巴巴的開源JSON解析庫,它能夠解析JSON格式的字符串,支持將Java Bean序列化爲JSON字符串,也能夠從JSON字符串反序列化到JavaBean。javascript

Fastjson使用場景

 

Fastjson已經被普遍使用在各類場景,包括cache存儲、RPC通信、MQ通信、網絡協議通信、Android客戶端、Ajax服務器處理程序等等。java

2.fastjson的優勢

2.1 速度快

fastjson相對其餘JSON庫的特色是快,從2011年fastjson發佈1.1.x版本以後,其性能從未被其餘Java實現的JSON庫超越。mysql

2.2 使用普遍

fastjson在阿里巴巴大規模使用,在數萬臺服務器上部署,fastjson在業界被普遍接受。在2012年被開源中國評選爲最受歡迎的國產開源軟件之一。sql

2.3 測試完備

fastjson有很是多的testcase,在1.2.11版本中,testcase超過3321個。每次發佈都會進行迴歸測試,保證質量穩定。json

2.4 使用簡單

fastjson的API十分簡潔。服務器

String text = JSON.toJSONString(obj); //序列化 VO vo = JSON.parseObject("{...}", VO.class); //反序列化 

2.5 功能完備

支持泛型,支持流處理超大文本,支持枚舉,支持序列化和反序列化擴展。網絡

如下例子對JSON.toJSONString和JSON.parseObject這2個重要方法進行測試:app

 

Fruit實體類:ide

 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)
相關文章
相關標籤/搜索