1. 简介
fastjson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将Java Bean序列化为JSON字符串,也可以从JSON字符串反序列化到JavaBean。
源码地址:https://github.com/alibaba/fastjson
- 依赖坐标
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
2. 使用
2.1 JSON对象
@Test
public void test01()
{
JSONObject jsonObject = new JSONObject();
// 添加元素
jsonObject.put("id", 2020001);
jsonObject.put("name", "周威");
jsonObject.put("sex", true);
jsonObject.put("birthday", new Date());
jsonObject.put("height", 1.73);
jsonObject.put("weight", 58F);
System.out.println("jsonObject = " + jsonObject);
// 删除元素
jsonObject.remove("height");
jsonObject.remove("wight");
jsonObject.remove("sex");
System.out.println("jsonObject = " + jsonObject);
}
- 添加结果
{
"birthday": 1600757856094,
"sex": true,
"name": "周威",
"weight": 58,
"id": 2020001,
"height": 1.73
}
- 删除结果
{
"birthday": 1600757856094,
"name": "周威",
"weight": 58,
"id": 2020001
}
2.2 JSON数组
@Test
public void test02()
{
JSONArray jsonArray = new JSONArray();
// 添加元素
jsonArray.add("A");
jsonArray.add(1);
jsonArray.add(true);
jsonArray.add(new Date());
System.out.println("jsonArray = " + jsonArray);
// 删除元素
jsonArray.remove(new Integer(1));
jsonArray.remove(2);
System.out.println("jsonArray = " + jsonArray);
}
- 添加结果
[
"A",
1,
true,
1600758192373
]
- 删除结果
[
"A",
true
]
2.3 Bean转JSON字符串
2.3.1 用户实体类
package zw.json.test;
import java.io.Serializable;
import java.util.Date;
/**
* @className User
* @description 用户实体类
* @author 周威
* @date 2020-9-22 13:54
*/
public class User implements Serializable
{
/* 用户编号 */
private Integer id;
/* 用户姓名 */
private String name;
/* 用户性别 */
private Boolean sex;
/* 用户生日 */
private Date birthday;
/* 用户身高 */
private Double height;
/* 用户体重 */
private Float weight;
/* 构造方法 */
public User()
{
}
public User(Integer id, String name, Boolean sex, Date birthday, Double height, Float weight)
{
this.id = id;
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.height = height;
this.weight = weight;
}
/* Getter和Setter方法 */
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Boolean getSex()
{
return sex;
}
public void setSex(Boolean sex)
{
this.sex = sex;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
public Double getHeight()
{
return height;
}
public void setHeight(Double height)
{
this.height = height;
}
public Float getWeight()
{
return weight;
}
public void setWeight(Float weight)
{
this.weight = weight;
}
/* toString方法 */
@Override
public String toString()
{
return "User{" + "id=" + id + ", name='" + name + '\'' + ", birthday=" + birthday + ", height=" + height + ", weight=" + weight + '}';
}
}
2.3.2 转化测试类
@Test
public void test03()
{
// 组装Bean
User user = new User();
user.setId(202001);
user.setName("周威");
user.setSex(true);
user.setBirthday(new Date());
user.setHeight(1.73);
user.setWeight(58F);
// 执行转化
String jsonString = JSON.toJSONString(user);
// 打印结果
System.out.println("jsonString = " + jsonString);
}
- 转化结果
{
"birthday": 1600758661102,
"height": 1.73,
"id": 202001,
"name": "周威",
"sex": true,
"weight": 58
}
2.4 JSON字符串转Bean
2.4.1 用户实体类
package zw.json.test;
import java.io.Serializable;
import java.util.Date;
/**
* @className User
* @description 用户实体类
* @author 周威
* @date 2020-9-22 13:54
*/
public class User implements Serializable
{
/* 用户编号 */
private Integer id;
/* 用户姓名 */
private String name;
/* 用户性别 */
private Boolean sex;
/* 用户生日 */
private Date birthday;
/* 用户身高 */
private Double height;
/* 用户体重 */
private Float weight;
/* 构造方法 */
public User()
{
}
public User(Integer id, String name, Boolean sex, Date birthday, Double height, Float weight)
{
this.id = id;
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.height = height;
this.weight = weight;
}
/* Getter和Setter方法 */
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Boolean getSex()
{
return sex;
}
public void setSex(Boolean sex)
{
this.sex = sex;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
public Double getHeight()
{
return height;
}
public void setHeight(Double height)
{
this.height = height;
}
public Float getWeight()
{
return weight;
}
public void setWeight(Float weight)
{
this.weight = weight;
}
/* toString方法 */
@Override
public String toString()
{
return "User{" + "id=" + id + ", name='" + name + '\'' + ", birthday=" + birthday + ", height=" + height + ", weight=" + weight + '}';
}
}
2.4.2 转化测试类
@Test
public void test04()
{
// 定义json
String jsonString = "{\"birthday\":1600754689038,\"height\":1.73,\"id\":202001,\"name\":\"周威\",\"sex\":true,\"weight\":58.0}";
// 执行转化
User user = JSON.parseObject(jsonString, User.class);
// 打印结果
System.out.println("user = " + user);
}
- 转化结果
2.5 Java集合转JSON数组
2.5.1 用户实体类
package zw.json.test;
import java.io.Serializable;
import java.util.Date;
/**
* @className User
* @description 用户实体类
* @author 周威
* @date 2020-9-22 13:54
*/
public class User implements Serializable
{
/* 用户编号 */
private Integer id;
/* 用户姓名 */
private String name;
/* 用户性别 */
private Boolean sex;
/* 用户生日 */
private Date birthday;
/* 用户身高 */
private Double height;
/* 用户体重 */
private Float weight;
/* 构造方法 */
public User()
{
}
public User(Integer id, String name, Boolean sex, Date birthday, Double height, Float weight)
{
this.id = id;
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.height = height;
this.weight = weight;
}
/* Getter和Setter方法 */
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Boolean getSex()
{
return sex;
}
public void setSex(Boolean sex)
{
this.sex = sex;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
public Double getHeight()
{
return height;
}
public void setHeight(Double height)
{
this.height = height;
}
public Float getWeight()
{
return weight;
}
public void setWeight(Float weight)
{
this.weight = weight;
}
/* toString方法 */
@Override
public String toString()
{
return "User{" + "id=" + id + ", name='" + name + '\'' + ", birthday=" + birthday + ", height=" + height + ", weight=" + weight + '}';
}
}
2.5.2 转化测试类
@Test
public void test05()
{
// 组装集合
List<User> list = new ArrayList<>();
list.add(new User(2020001, "张三", true, new Date(), 1.73, 58F));
list.add(new User(2020002, "李四", false, new Date(), 1.65, 53F));
// 执行转化
String jsonArrayString = JSONArray.toJSONString(list);
// 打印结果
System.out.println("jsonArrayString = " + jsonArrayString);
}
- 转化结果
[
{
"birthday": 1600759478923,
"height": 1.73,
"id": 2020001,
"name": "张三",
"sex": true,
"weight": 58
},
{
"birthday": 1600759478923,
"height": 1.65,
"id": 2020002,
"name": "李四",
"sex": false,
"weight": 53
}
]
2.6 JSON数组转Java
2.6.1 用户实体类
package zw.json.test;
import java.io.Serializable;
import java.util.Date;
/**
* @className User
* @description 用户实体类
* @author 周威
* @date 2020-9-22 13:54
*/
public class User implements Serializable
{
/* 用户编号 */
private Integer id;
/* 用户姓名 */
private String name;
/* 用户性别 */
private Boolean sex;
/* 用户生日 */
private Date birthday;
/* 用户身高 */
private Double height;
/* 用户体重 */
private Float weight;
/* 构造方法 */
public User()
{
}
public User(Integer id, String name, Boolean sex, Date birthday, Double height, Float weight)
{
this.id = id;
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.height = height;
this.weight = weight;
}
/* Getter和Setter方法 */
public Integer getId()
{
return id;
}
public void setId(Integer id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Boolean getSex()
{
return sex;
}
public void setSex(Boolean sex)
{
this.sex = sex;
}
public Date getBirthday()
{
return birthday;
}
public void setBirthday(Date birthday)
{
this.birthday = birthday;
}
public Double getHeight()
{
return height;
}
public void setHeight(Double height)
{
this.height = height;
}
public Float getWeight()
{
return weight;
}
public void setWeight(Float weight)
{
this.weight = weight;
}
/* toString方法 */
@Override
public String toString()
{
return "User{" + "id=" + id + ", name='" + name + '\'' + ", birthday=" + birthday + ", height=" + height + ", weight=" + weight + '}';
}
}
2.6.2 转化测试类
@Test
public void test06()
{
// 定义json
String jsonArrayString = "[{\"birthday\":1600756263818,\"height\":1.73,\"id\":2020001,\"name\":\"张三\",\"sex\":true,\"weight\":58.0},{\"birthday\":1600756263818,\"height\":1.65,\"id\":2020002,\"name\":\"李四\",\"sex\":false,\"weight\":53.0}]";
// 开始转化
List<User> list = JSONArray.parseArray(jsonArrayString, User.class);
// 打印结果
System.out.println("list = " + list);
}
- 转化结果