zoukankan      html  css  js  c++  java
  • Jackson使用

    1. (1),在maven中导入相关依赖

      <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
              <dependency>
                  <groupId>com.fasterxml.jackson.core</groupId>
                  <artifactId>jackson-databind</artifactId>
                  <version>2.9.8</version>
              </dependency>
      

      (2).在springBoot中已经集成了jackson

    2. 使用

      核心类:ObjectMapper mapper = new ObjectMapper();

      相关操作:mapper.writeValueAsString(object);

    3. 时间:

      (1).默认为时间戳(公元19701月1日开始)

      (2).转换为yyyy-MM-dd HH:mm:ss格式

      //关闭使用时间戳
      mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
      //设定一个新格式
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      
    4. 封装成工具类使用

      public static ObjectMapper mapper = new ObjectMapper();
      
      //默认不是以"yyyy-MM-dd hh:MM:ss"的格式输出
      public static String toJsonString(Object object){
          return toJsonString(object,null);
      }
      
      public static String toJsonString(Object object, String dateFormat){
          if(dateFormat != null){
              //事件戳,不使用
              mapper.configure(SerializationFeature.WRITE_DATE_KEYS_AS_TIMESTAMPS,false);
              //使用自定义的时间格式
              SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
          }
          try{
              return mapper.writeValueAsString(object);
          } catch (JsonProcessingException e) {
              e.printStackTrace();;
          }
          return null;
      }
      
  • 相关阅读:
    StatusStrip控件的使用(转:http://blog.sina.com.cn/s/blog_4f18c3ec0100fguf.html)
    linux根文件系统
    git使用技巧
    修改git用户名
    luci中添加application
    openwrt安装依赖库
    STM32(二十九)定时器介绍
    openwrt部分文件解析
    uci.js文件解析
    矿机算力
  • 原文地址:https://www.cnblogs.com/Arno-vc/p/13509602.html
Copyright © 2011-2022 走看看