筆記_使用FastJson匯出JSON
阿新 • • 發佈:2020-08-11
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 =