FastJson
概要
-
速度:
-
fastjson是目前java语言中最快的json库,比自称最快的jackson速度要快
-
fastjson比gson快大约6倍,
-
-
网站:
FastJson常用方法
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray
public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject
public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean
public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray
public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合
public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本
public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本
public static final Object toJSON(Object javaObject); //将JavaBean转换为JSONObject或者JSONArray。
Java ==> JSON
步骤
-
step1. 导入jar包
-
step2. 构建POJO结构
-
step3. 将POJO转换为JSON字符串:
-
JSON.toJSONString( 顶级POJO对象的引用 )
=> 纯字符串,只有一行 -
JSON.toJSONString( 顶级POJO对象的引用, true)
=> 带格式
-
常用的方法
// 1)将JavaBean序列化为JSON文本
public static final String toJSONString(Object object);
// 2)将JavaBean序列化为带格式的JSON文本
public static final String toJSONString(Object object, boolean prettyFormat);
一个简单的Demo
-
User.java
package josn.simple;
public class User {
private String username;
private String password;
public User() {}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
-
UserGroup.java
package josn.simple;
import java.util.ArrayList;
import java.util.List;
public class UserGroup {
private String name;
private List<User> users;
public UserGroup() {
users = new ArrayList<User>();
}
public UserGroup(String name, List<User> users) {
super();
this.name = name;
this.users = users;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
-
TestFastJson.java
package josn.simple;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
public class TestFastJson {
-
控制台输出结果:
简单Java类转Json字符串:{"password":"123456","username":"ali"}
List<Object>转Json字符串:[{"password":"123456","username":"coco"},{"password":"789","username":"ly"}]
复杂的Java类转Json字符串:{"name":"userGroup","users":[{"password":"123456","username":"coco"},{"password":"789","username":"ly"}]}
我的一个Demo
-
User.java
package josn.mydemo;
public class User {
private int id;
private String name;
private Computer computer;
public User() {
super();
}
public User(int id, String name, Computer computer) {
super();
this.id = id;
this.name = name;
this.computer = computer;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Computer getComputer() {
return computer;
}
public void setComputer(Computer computer) {
this.computer = computer;
}
-
Computer.java
package josn.mydemo;
public class Computer {
private String brand;
private String CPUdegree;
public Computer() {
super();
}
public Computer(String brand, String cPUdegree) {
super();
this.brand = brand;
CPUdegree = cPUdegree;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public String getCPUdegree() {
return CPUdegree;
}
public void setCPUdegree(String cPUdegree) {
CPUdegree = cPUdegree;
}
-
TestObject.java
package josn.mydemo;
import org.junit.Test;
import com.alibaba.fastjson.JSON;
public class TestObject {
-
结果:
练习题:一个复杂的Demo
题目
[
{
"id": "user_list",
"key": "id",
"tableName": "用户列表",
"className": "cn.dmego.domain.User",
"column": [
{
"key": "rowIndex",
"header": "序号",
"width": "50",
"allowSort": "false"
},
{
"key": "id",
"header": "id",
"hidden": "true"
},
{
"key": "name",
"header": "姓名",
"width": "100",
"allowSort": "true"
}
]
},
{
"id": "role_list",
"key": "id",
"tableName": "角色列表",
"className": "cn.dmego.domain.Role",
"column": [
{
"key": "rowIndex",
"header": "序号",
"width": "50",
"allowSort": "false"
},
{
"key": "id",
"header": "id",
"hidden": "true"
},
{
"key": "name",
"header": "名称",
"width": "100",
"allowSort": "true"
}
]
}
]
解答
-
Column.java
package josn.complex;
public class Column {
private String key;
private String header;
private Integer width;
private Boolean allowSort;
private Boolean hidden;
public Column() {
}
public Column(String key, String header, Boolean hidden) {
super();
this.key = key;
this.header = header;
this.hidden = hidden;
}
public Column(String key, String header, Integer width, Boolean allowSort) {
super();
this.key = key;
this.header = header;
this.width = width;
this.allowSort = allowSort;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getHeader() {
return header;
}
public void setHeader(String header) {
this.header = header;
}
public Integer getWidth() {
return width;
}
public void setWidth(Integer width) {
this.width = width;
}
public Boolean isAllowSort() {
return allowSort;
}
public void setAllowSort(Boolean allowSort) {
this.allowSort = allowSort;