柏竹 柏竹
首页
后端
前端
  • 应用推荐
关于
友链
  • 分类
  • 标签
  • 归档

柏竹

奋斗柏竹
首页
后端
前端
  • 应用推荐
关于
友链
  • 分类
  • 标签
  • 归档
  • Java基础

  • JavaWeb

  • 拓展技术

  • 框架技术

  • 数据库

  • 数据结构

  • Spring

  • SpringMVC

  • SpringBoot

    • SpringBoot
    • SpringBoot3基础特性
    • SpringBoot3核心原理
    • 框架整合

      • SpringBoot SpringMVC 整合
      • SpringBoot JDBC 整合
      • SpringBoot MyBatis 整合
      • SpringBoot tk-MyBatis 整合
      • SpringBoot Shiro 整合
      • SpringBoot Redis 整合
      • SpringBoot MyBatisPlus 整合
      • SpringBoot JSON 整合
        • JSON整合
          • 对象>JSON
          • JSON>对象
          • 自定义工具类
      • SpringBoot Thymeleaf 整合
      • 整合WebSocket实现聊天功能
    • SpringBoot部署
  • SpringClound

  • Ruoyi-Vue-Plus

  • 后端
  • SpringBoot
  • 框架整合
Bozhu12
2023-06-10
目录

SpringBoot JSON 整合

# JSON整合

SpringBoot 自带有 jackson 和 gson , 一般JSON操作是通过 ObjectMapper对象 进行操作(工具类)

SpringBoot 一般情况是使用自动注入形式调用 ObjectMapper对象

# 对象>JSON

User user = new User();
String userJson = new ObjectMapper().writeValueAsString(user);
//...

# JSON>对象

// 单对象
String json = "{name:'Sans', age:22}";
User user = new ObjectMapper().readValue(josn, User.class);
// 数组
String jsonArrayStr = "[{\"id\":\"123\",\"name\":\"张三\"},{\"id\":\"456\",\"name\":\"李四\"}]";
List<Person> personList = objectMapper.readValue(jsonArrayStr, new TypeReference<List<Person>>() {});

# 自定义工具类

代码展开
public class JsonUtil {

    public static final ObjectMapper objectMapper = new ObjectMapper();

    static {
        //忽略字段不匹配错误
 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    /**
     * 创建 ObjectNode
     * @return
     */
    public static ObjectNode createJson() {
        return objectMapper.createObjectNode();
    }

    /**
     * 字符串转 java bean
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T string2Bean(String json, Class<T> clazz){
        T t = null;
        try {
            t = objectMapper.readValue(json,clazz);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return t;
    }

    /**
     * 字符串转 Map
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> Map<String,Object> string2Map(String json, Class<T> clazz){
        Map<String,Object> map = null;
        try {
            map = objectMapper.readValue(json, new TypeReference<Map<String,Object>>() {});
        } catch (JsonProcessingException e) {
            map = Collections.emptyMap();
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 字符串转 List<Bean>
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> string2BeanList(String json, Class<T> clazz){
        List<T> t = null;
        try {
            t = objectMapper.readValue(json, new TypeReference<List<T>>() {});
        } catch (JsonProcessingException e) {
            t = Collections.emptyList();
            e.printStackTrace();
        }
        return t;
    }

    /**
     * 字符串转 Bean[]
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T[] string2BeanArray(String json, Class<T> clazz){
        T[] t = null;
        try {
            t = objectMapper.readValue(json, new TypeReference<T[]>() {});
        } catch (JsonProcessingException e) {
            t = (T[])new Object[0];
            e.printStackTrace();
        }
        return t;
    }

    /**
     * 字符串转 JsonNode
     * @param json
     * @return
     */
    public static JsonNode string2Json(String json) {
        JsonNode jsonNode = null;
        try {
            jsonNode = objectMapper.readTree(json);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return jsonNode;
    }

    /**
     * java bean 或者 Map 或者 JsonNode 转字符串
     * @param o
     * @return
     */
    public static String object2String(Object o) {
        String json = null;
        try {
            json = objectMapper.writeValueAsString(o);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * java bean 或者 Map 或者 JsonNode 转 JsonNode
     * @param o
     * @return
     */
    public static JsonNode object2Json(Object o) {
        JsonNode jsonNode = null;
        try {
            String jsonString = objectMapper.writeValueAsString(o);
            jsonNode = objectMapper.readTree(jsonString);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return jsonNode;
    }

    /**
     * jsonNode 转 JsonNode
     * @param jsonNode
     * @return
     */
    public static <T> T json2Bean(JsonNode jsonNode, Class<T> clazz) {
        String json = jsonNode.toString();
        return string2Bean(json, clazz);
    }
}

应用实例

// 数组
public static void main(String[] args) {
    ObjectNode person = objectMapper.createObjectNode();
    person.put("id","1101");
    person.put("name","张三");
    person.put("age",35);
    person.put("sex","男");

    List<ObjectNode> children = new ArrayList<>(1);
    ObjectNode child = objectMapper.createObjectNode();
    child.put("id","1102");
    child.put("name","张小三");
    child.put("age",12);
    child.put("sex","男");

    children.add(child);

    person.putArray("children").addAll(children);

    System.out.println(person.toPrettyString());
}
#JSON

← SpringBoot MyBatisPlus 整合 SpringBoot Thymeleaf 整合→

最近更新
01
HTTPS自动续签
10-21
02
博客搭建-简化版(脚本)
10-20
03
ruoyi-vue-plus-部署篇
07-13
更多文章>
Theme by Vdoing | Copyright © 2019-2024 | 桂ICP备2022009417号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式