更新时间:2018年12月13日14时38分 来源:传智播客 浏览次数:
Fastjson是一个Java语言编写的高性能功能完善的JSON库。它采用一种“假定有序快速匹配”的算法,把JSON Parse的性能提升到极致,是目前Java语言中最快的JSON库。Fastjson接口简单易用,已经被广泛使用在缓存序列化、协议交互、Web输出、Android客户端等多种应用场景。
maven工程可以在pom文件加入依赖
<
dependency
>
<
groupId
>com.alibaba</
groupId
>
<
artifactId
>fastjson</
artifactId
>
<
version
>1.2.21</
version
>
</
dependency
>
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);
//序列化:
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>>() {});
下面贴出自己代码,相当于是FastJson使用操作的一个集合,基本上满足开发中绝大部分的json解析和拼接了。
package
heima.itcast.demo;
import
com.alibaba.fastjson.JSON;
import
com.alibaba.fastjson.JSONObject;
import
com.alibaba.fastjson.TypeReference;
import
heima.itcast.demo.model.Book;
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[i].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);
}
}
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;
}
}
北京校区