FastJSON使用笔记

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);
}
  • 转化结果

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Fastjson是一个Java语言编写的高性能JSON处理库,可以将Java对象与JSON数据进行相互转换。下面是一个简单的示例代码,展示了如何使用Fastjson进行JSON的序列化和反序列化: 1. 添加Fastjson的依赖 在Maven项目中,可以在pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.76</version> </dependency> ``` 2. JSON序列化 ```java import com.alibaba.fastjson.JSON; public class FastjsonSerializer { public static void main(String[] args) { // 创建一个Java对象 User user = new User("John", 25); // 将Java对象序列化为JSON字符串 String jsonString = JSON.toJSONString(user); System.out.println(jsonString); } } class User { private String name; private int age; public User(String name, int age) { this.name = name; this.age = age; } // 省略getter和setter方法 } ``` 3. JSON反序列化 ```java import com.alibaba.fastjson.JSON; public class FastjsonDeserializer { public static void main(String[] args) { // JSON字符串 String jsonString = "{\"name\":\"John\",\"age\":25}"; // 将JSON字符串反序列化为Java对象 User user = JSON.parseObject(jsonString, User.class); // 使用反序列化后的Java对象 System.out.println(user.getName()); System.out.println(user.getAge()); } } class User { private String name; private int age; // 省略构造方法和getter、setter方法 } ``` 以上代码示例了Fastjson的基本用法,你可以根据自己的需求进行更复杂的操作。请注意,在实际使用中,要确保导入了Fastjson的相关类或包,并且使用正确的版本。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值