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

2025-05-29 0 81

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

测试:

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

集合转化JSON

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

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

测试:

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

Map转化JSON

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

前端结果是:对象

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

new Date() 转化JSON

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

前端结果是:时间戳

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

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

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

封装

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

二、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. @RequestMapping(value = "/json")
  4. public String json() throws Exception{
  5. List<User> userList = new ArrayList<>();
  6. userList.add(new User("1", "1", 20));
  7. String res = JSON.toJSONString(userList);
  8. return res;
  9. }

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

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

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

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java中常用解析工具jackson及fastjson的使用 https://www.kuaiidc.com/103987.html

相关文章

发表评论
暂无评论