FastJSON的使用

JSON

JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。

介绍

FastJson是阿里巴巴的开源JSON解析库,它可以解析JSON格式的字符串,支持将java Bean序列化为JSON字符串,也支持反序列化。

FastJson序列化API

序列化:是指将Java对象转成json格式字符串的过程,javaBean对象,List集合对象,Map集合,为应用中最为广泛的。

javaBean对象转json

JSON.toJSONString(student),传入一个Student类型的对象,返回JSON格式的字符串

首先创建一个Student

@Data
public class Student {
    private String name;
    private Integer age;
    private Date birthday;
}

测试类

@Test
public void javaBeanToJson() {
    Student student = new Student();
    student.setName("张三");
    student.setAge(25);
    student.setBirthday(new Date());

    // 转Json
    String jsonString = JSON.toJSONString(student);
    System.out.println(jsonString);
}

结果

 {"age":25,"birthday":1630161526496,"name":"张三"}

List集合转Json

测试

@Test
public void listToJson() {
    ArrayList<Student> students = new ArrayList<>();
    Student student1 = new Student();
    student1.setName("张三");
    student1.setAge(25);
    student1.setBirthday(new Date());

    Student student2 = new Student();
    student2.setName("李四");
    student2.setAge(22);
    student2.setBirthday(new Date());
    students.add(student1);
    students.add(student2);

    // list集合转json
    String jsonString = JSON.toJSONString(students);
    System.out.println(jsonString);
}

结果

[{"age":25,"birthday":1630162020659,"name":"张三"},
{"age":22,"birthday":1630162020659,"name":"李四"}]

Map转Json

@Test
public void mapToJSON() {
	Map<String, Student> map = new HashMap<String, Student>();
    Student student1 = new Student();
    student1.setName("张三");
    student1.setAge(25);
    student1.setBirthday(new Date());

    Student student2 = new Student();
    student2.setName("李四");
    student2.setAge(22);
    student2.setBirthday(new Date());
    map.put("student1", student1);
    map.put("student2", student2);
    String string = JSON.toJSONString(map);
    System.out.printf(string);
}

结果

{"student2":{"age":22,"birthday":1630288166573,"name":"李四"},"student1":{"age":25,"birthday":1630288166573,"name":"张三"}}

FastJson反序列化API

注意:从前端传过来特殊符号会被转义,因此在转换之前需要将转义字符替换为原来的字符

JSON字符串转Java对象

JSON.parseObject(String text, Student.class)
text:要转化的JSON字符串
class:实体类的class对象
return:Student的对象

@Test
public void stringToJavaBean() {
    String jsonString = "{\"age\":25,\"birthday\":1630162020659,\"name\":\"张三\"}";
    Student student = JSON.parseObject(jsonString, Student.class);
    System.out.println(student);
}

结果

Student(name=张三, age=25, birthday=Sat Aug 28 22:47:00 CST 2021)

JSON字符串转List集合

JSON.parseArray(String text, Student)
text:要转化的JSON字符串
student:内的泛型的class对象
return:List< Student >

@Test
public void stringToArray() {
    String jsonString = "[{\"age\":25,\"birthday\":1630162020659,\"name\":\"张三\"},{\"age\":22,\"birthday\":1630162020659,\"name\":\"李四\"}]";
    List<Student> students = JSON.parseArray(jsonString, Student.class);
    students.forEach(System.out::println);
}

结果

Student(name=张三, age=25, birthday=Sat Aug 28 22:47:00 CST 2021)
Student(name=李四, age=22, birthday=Sat Aug 28 22:47:00 CST 2021)

JSON字符串转Map集合

JSON.parseObject(String text, new TypeReference<Map<String, Student>>{})
注意:如果不加参数,Map集合是没有泛型的,没有泛型的集合是不安全的
在TypeReference的泛型中,传递转后的Map集合,不要忘记“{}”

@Test
public void stringToMap() {
    String jsonString = "{\"student2\":{\"age\":22,\"birthday\":1630288166573,\"name\":\"李四\"},\"student1\":{\"age\":25,\"birthday\":1630288166573,\"name\":\"张三\"}}";
    Map<String, Student> map = JSON.parseObject(jsonString, new TypeReference<Map<String, Student>>() {
    });
    for (String key : map.keySet()) {
        System.out.println(key + ":" + map.get(key));
    }
}

结果:

student2:Student(name=李四, age=22, birthday=Mon Aug 30 09:49:26 CST 2021)
student1:Student(name=张三, age=25, birthday=Mon Aug 30 09:49:26 CST 2021)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值