JAVA操作Json的ObjectMapper类

JAVA操作Json的ObjectMapper类

  • 市面上用于在 Java 中解析 Json 的第三方库,随便一搜不下几十种,其中的佼佼者有 Google 的 Gson以及本文的 jackson。

  • 三者不相伯仲,随着掌握一个都能满足项目中的 json 解析操作,因为 Spring Boot Web 组件默认使用的是 jackson,所以掌握 Jackjson 是非常有必要的。

复制代码
  package com.zving;
   
  import org.codehaus.jackson.map.ObjectMapper;
   
  /**
   * 学习ObjectMapper类
   * 
   * @author Clover
   */
  public class JacksonTester {
   
      public static void main(String[] args) {
          ObjectMapper mapper = new ObjectMapper();
          //指定遇到date按照这种格式转换        
          SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        
          mapper.setDateFormat(fmt);
          String jsonString = "";
          stringToStudent(mapper, jsonString);
          studentToString(mapper, jsonString);
          stringToStudentList(mapper, jsonString);
      }
   
      /**
       * 字符串转实体类
       * 
       * @param mapper
       * @param jsonString
       */
      public static void stringToStudent(ObjectMapper mapper, String jsonString) {
          jsonString = "{\"id\":1001,\"name\":\"Tom\",\"info\":\"一个小男孩\"}";
   
          try {
              Student student = mapper.readValue(jsonString, Student.class);
              System.out.println(student);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
   
      /**
       * 实体类转字符串
       * 
       * @param mapper
       * @param jsonString
       */
      public static void studentToString(ObjectMapper mapper, String jsonString) {
   
          Student student = new Student();
          student.setId(1002);
          student.setName("李四");
          student.setInfo("一个小女孩");
          try {
              jsonString = mapper.writeValueAsString(student);
              System.out.println(jsonString);
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
   
      /**
       * 字符串转 List<实体类>
       * 
       * @param mapper
       * @param jsonString
       */
      public static void stringToStudentList(ObjectMapper mapper, String jsonString) {
          jsonString = "[{\"id\":1001,\"name\":\"Tom\",\"info\":\"一个小男孩\"},
  			{\"id\":1002,\"name\":\"Jane\",\"info\":\"一个小女孩\"}]";
  							
          try {
              Student[] stu = mapper.readValue(jsonString, Student[].class);
              for (Student student : stu) {
                  System.out.println(student);
              }
   
              String ss = mapper.writeValueAsString(stu);
              System.out.println(ss);
   
          } catch (Exception e) {
              e.printStackTrace();
          }
      }
   
  }
相关推荐
Dcs5 小时前
Java 中 UnaryOperator 接口与 Lambda 表达式的应用示例
java·后端
猫小呆6 小时前
Weaviate服务器部署笔记
服务器·weaviate
M158227690556 小时前
工业互联利器!EtherNet/IP 转 ModbusTCP 网关,让跨协议通信零门槛
服务器·网络·tcp/ip
阿巴~阿巴~6 小时前
基于UDP协议的英汉翻译服务系统:从网络通信到字典查询的完整机制
linux·服务器·网络·网络协议·udp协议·套接字绑定·英汉翻译服务系统
阿巴~阿巴~6 小时前
简易回声服务器实现与网络测试指南
linux·服务器·网络·udp协议·网络测试·udp套接字编程
bagadesu7 小时前
使用Docker构建Node.js应用的详细指南
java·后端
没有bug.的程序员7 小时前
Spring Cloud Gateway 性能优化与限流设计
java·spring boot·spring·nacos·性能优化·gateway·springcloud
molunnnn7 小时前
第四章 Agent的几种经典范式
开发语言·python
洛_尘8 小时前
JAVA EE初阶 2: 多线程-初阶
java·开发语言