Fastjson是一個Java語言編寫的高性能功能完善的JSON庫。它採用一種「假定有序快速匹配」的算法,把JSON Parse的性能提高到極致,是目前Java語言中最快的JSON庫。Fastjson接口簡單易用,已經被普遍使用在緩存序列化、協議交互、Web輸出、Android客戶端等多種應用場景。java
maven工程能夠在pom文件加入依賴算法
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.21</version>
</dependency> json
fastjson入口類是com.alibaba.fastjson.JSON,主要的API是JSON.toJSONString,和parseObject。數組
經常使用的序列化操做均可以在JSON類上的靜態方法直接完成。緩存
// 把JSON文本parse爲JSONObject或者JSONArray
public static final Object parse(String text);
// 把JSON文本parse成JSONObject
public static final JSONObject parseObject(String text);
// 把JSON文本parse爲JavaBean
public static final <T> T parseObject(String text, Class<T> clazz);
// 把JSON文本parse成JSONArray
public static final JSONArray parseArray(String text);
//把JSON文本parse成JavaBean集合
public static final <T> List<T> parseArray(String text, Class<T> clazz);
// 將JavaBean序列化爲JSON文本
public static final String toJSONString(Object object);
// 將JavaBean序列化爲帶格式的JSON文本
public static final String toJSONString(Object object, boolean prettyFormat);
//將JavaBean轉換爲JSONObject或者JSONArray。
public static final Object toJSON(Object javaObject); app
-----------------------------------------------------------
//序列化:
String jsonString = JSON.toJSONString(obj);
//反序列化:
VO vo = JSON.parseObject("...", VO.class);
//泛型反序列化:
import com.alibaba.fastjson.TypeReference;
List<VO> list = JSON.parseObject("...", new TypeReference<List<VO>>() {}); maven
下面貼出本身代碼,至關因而FastJson使用操做的一個集合,基本上知足開發中絕大部分的json解析和拼接了。性能
package heima.itcast.demo; ui
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import heima.itcast.demo.model.Book; this
import java.util.*;
public class Test {
public static void main(String[] args) {
//String轉化爲實體類Book
StrToObject();
//String轉化爲jsonObject
StrToJsonObject();
//實體類轉換爲json
//多個實體類轉換爲json數組形式的string
//String轉化爲List<Book>
StrToListObject();
//String轉化爲數組
//String轉化爲ArrayList
StrToArrayList();
//map和json互相轉換
mapTo();
}
/**
* String轉化爲實體類Book
*/
public static void StrToObject() {
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append("\"name\": \"教父三部曲\",");
sb.append(" \"author\": \"馬里奧·普佐\",");
sb.append(" \"price\": \"100\"");
sb.append("}");
Book book = JSON.parseObject(sb.toString(), Book.class);
System.out.println("書名:"+book.getName()+";做者:"+book.getAuthor()+";價格:"+book.getPrice());
}
/**
* String轉化爲jsonObject
*/
public static void StrToJsonObject() {
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append("\"name\": \"教父三部曲\",");
sb.append(" \"author\": \"馬里奧·普佐\",");
sb.append(" \"price\": \"100\"");
sb.append("}");
JSONObject jsonObj = JSON.parseObject(sb.toString());
for(Map.Entry<String, Object> entry : jsonObj.entrySet()){
System.out.println(entry.getKey()+"--"+entry.getValue());
}
}
/**
* 實體類轉換爲json
* 多個實體類轉換爲json數組形式的string
* String轉化爲List<Book>
*/
public static void StrToListObject() {
List<Book> bookList = new ArrayList<>();
Book book1 = new Book("教父三部曲","馬里奧·普佐",100) ;
Book book2 = new Book("天才在左瘋子在右","高銘",30) ;
bookList.add(book1);
bookList.add(book2);
//實體類轉換爲json
String book1Str = JSON.toJSONString(book1);
//多個實體類轉換爲json數組形式的string
String bookStr = JSON.toJSONString(bookList);
//String轉化爲List<Book>
List<Book> bookList3 = JSON.parseArray(bookStr,Book.class);
}
/**
* String轉化爲數組
* String轉化爲ArrayList
*/
public static void StrToArrayList(){
StringBuilder sb = new StringBuilder();
sb.append("[{");
sb.append("\"name\": \"教父三部曲\",");
sb.append(" \"author\": \"馬里奧·普佐\",");
sb.append(" \"price\": \"100\"");
sb.append("}]");
// String轉化爲數組
Book[] arrBook = JSON.parseObject(sb.toString(),new TypeReference<Book[]>(){});
List<Book> list = Arrays.asList(arrBook);
for (int i = 0; i < arrBook.length; i++) {
System.out.println(arrBook_.toString());__
}
// String轉化爲ArrayList
ArrayList<Book> bookList= JSON.parseObject(sb.toString(),new TypeReference<ArrayList<Book>>(){});
for(Book book : bookList){
System.out.println(book.toString());
}
}
/**
* map和json互相轉換
*/
public static void mapTo(){
Map<String,String> map = new HashMap<>();
map.put("name", "教父三部曲");
map.put("author", "馬里奧·普佐");
map.put("price", "100");
//map轉換爲json
String json = JSON.toJSONString(map);
//json轉換爲map
Map map1 = JSON.parseObject(json);
System.out.println(map1.toString());
Map<String,String> map2 = (Map<String,String>)JSON.parse(json);
}
}
附Book類代碼
package heima.itcast.demo.model;
public class Book {
private String name;
private String author;
private int price;
public Book() {
}
public Book(String name, String author, int price) {
this.name = name;
this.author = author;
this.price = price;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public int getPrice() {
return price;
}
public void setPrice(int price) { this.price = price; } }_