Java中常用解析工具jackson及fastjson的使用

时间:2022-07-07 04:12:29

一、maven安装jackson依赖

  1. <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
  2. <dependency>
  3. <groupId>com.fasterxml.jackson.core</groupId>
  4. <artifactId>jackson-databind</artifactId>
  5. <version>2.12.3</version>
  6. </dependency>

二、Jackson的使用

实体类转化JSON

把实体类转化为JSON格式数据,返回给前端

创建 ObjectMapper obj = new ObjectMapper(); 对象,对象的 writeValueAsString 方法 会把一个实体类(必须有get、set方法)转化为JSON对象。

  1. package com.lxc.springboot.controller;
  2.  
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6.  
  7. @RestController // 这个类下边的所有方法,都会返回json,不会返回一个视图!
  8. public class Json {
  9.  
  10. @RequestMapping(value = "/json")
  11. public String json() throws Exception{
  12. User user = new User("吕星辰", "888", 20);
  13. ObjectMapper obj = new ObjectMapper();
  14. String jsonObject = obj.writeValueAsString(user);
  15. return jsonObject;
  16. }
  17. // 为测试方便,在这里写一个实体类
  18. public static class User {
  19. private String userName;
  20.  
  21. public String getUserName() {
  22. return userName;
  23. }
  24.  
  25. public void setUserName(String userName) {
  26. this.userName = userName;
  27. }
  28.  
  29. public String getPassword() {
  30. return password;
  31. }
  32.  
  33. public void setPassword(String password) {
  34. this.password = password;
  35. }
  36.  
  37. public int getAge() {
  38. return age;
  39. }
  40.  
  41. public void setAge(int age) {
  42. this.age = age;
  43. }
  44.  
  45. private String password;
  46. private int age;
  47. public User(String userName, String password, int age) {
  48. this.userName = userName;
  49. this.password = password;
  50. this.age = age;
  51. }
  52. }
  53. }

测试:

Java中常用解析工具jackson及fastjson的使用

集合转化JSON

前端结果是:一个数组,里边是一个个对象

  1. package com.lxc.springboot.controller;
  2.  
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6.  
  7. import java.util.ArrayList;
  8. import java.util.List;
  9.  
  10. @RestController
  11. public class Json {
  12.  
  13. @RequestMapping(value = "/json")
  14. public String json() throws Exception{
  15. // 创建一个集合
  16. List<User> userList = new ArrayList<>();
  17. for(int i = 0; i < 3; i ++) {
  18. userList.add(new User("用户名"+i, "密码"+i, 20+i));
  19. }
  20. ObjectMapper obj = new ObjectMapper();
  21. String jsonObject = obj.writeValueAsString(userList);
  22. return jsonObject;
  23. }
  24. // 上边有实体类,这里省略
  25. }

测试:

Java中常用解析工具jackson及fastjson的使用

Map转化JSON

  1. @RestController
  2. public class Json {
  3.  
  4. @RequestMapping(value = "/json")
  5. public String json() throws Exception{
  6. // 创建一个Map
  7. Map<String, Object> map = new HashMap<>();
  8. map.put("name", "测试名");
  9. map.put("age", 20);
  10. ObjectMapper obj = new ObjectMapper();
  11. String jsonObject = obj.writeValueAsString(map);
  12. return jsonObject;
  13. }
  14. }

前端结果是:对象

Java中常用解析工具jackson及fastjson的使用

new Date() 转化JSON

  1. @RestController
  2. public class Json {
  3.  
  4. @RequestMapping(value = "/json")
  5. public String json() throws Exception{
  6. Date date = new Date();
  7. ObjectMapper obj = new ObjectMapper();
  8. String jsonObject = obj.writeValueAsString(date);
  9. return jsonObject;
  10. }
  11. }

前端结果是:时间戳

Java中常用解析工具jackson及fastjson的使用

当然,也可以自定义时间格式

  1. @RestController
  2. public class Json {
  3.  
  4. @RequestMapping(value = "/json")
  5. public String json() throws Exception{
  6. Date date = new Date();
  7. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
  8. String time = sdf.format(date); // "2021-06-27 05:19:33"
  9. ObjectMapper obj = new ObjectMapper();
  10. String jsonObject = obj.writeValueAsString(time);
  11. return jsonObject;
  12. }
  13. }

封装

  1. package com.lxc.springboot.utils;
  2.  
  3. import com.fasterxml.jackson.core.JsonProcessingException;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.fasterxml.jackson.databind.SerializationFeature;
  6.  
  7. import java.text.SimpleDateFormat;
  8.  
  9. public class JavaUtils {
  10. /**
  11. * 使用下边方法需要导入依赖包:
  12. * <dependency>
  13. * <groupId>com.fasterxml.jackson.core</groupId>
  14. * <artifactId>jackson-databind</artifactId>
  15. * <version>2.12.3</version>
  16. * </dependency>
  17. *
  18. * @param object 集合(List)、Map(HashMap)、对象(new Date)
  19. * @param format 时间格式化 yyyy-MM-dd hh:mm:ss
  20. * @return JSON格式化的字符串
  21. */
  22. public static String getJson(Object object, String format) {
  23. ObjectMapper objectMapper = new ObjectMapper();
  24. // 不使用时间戳的方式
  25. objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
  26. // 自定义时间格式
  27. SimpleDateFormat sdf = new SimpleDateFormat(format);
  28. // 设置时间格式化
  29. objectMapper.setDateFormat(sdf);
  30. try {
  31. String jsonValue = objectMapper.writeValueAsString(object);
  32. return jsonValue;
  33. } catch (JsonProcessingException e) {
  34. e.printStackTrace();
  35. }
  36. return null;
  37. }
  38. public static String getJson(Object object) {
  39. return getJson(object, "yyyy-MM-dd hh:mm:ss");
  40. }
  41. }

二、FastJson的使用

使用maven导入依赖包

  1. <!--下边依赖跟aop没关系,只是项目中用到了 JSONObject,所以引入fastjson-->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>fastjson</artifactId>
  5. <version>1.2.70</version>
  6. </dependency>

常用方法:

(1)JSON.toJSONString(obejct) - java对象转JSON字符串

(2)JSON.parseObject(string, User.class) - JSON字符串转java对象

使用

  1. @RestController
  2. public class Json {
  3.  
  4. @RequestMapping(value = "/json")
  5. public String json() throws Exception{
  6. List<User> userList = new ArrayList<>();
  7. userList.add(new User("1", "1", 20));
  8. String res = JSON.toJSONString(userList);
  9. return res;
  10. }

Java中常用解析工具jackson及fastjson的使用

到此这篇关于Java中常用解析工具jackson及fastjson的使用的文章就介绍到这了,更多相关jackson和fastjson的使用内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_42778001/article/details/118270837