zoukankan      html  css  js  c++  java
  • jackson工具类(JsonUtil )

    springboot用的就是这个

    import com.fasterxml.jackson.core.JsonParser;
    import com.fasterxml.jackson.core.type.TypeReference;
    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.databind.JavaType;
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import java.io.*;
    import java.util.ArrayList;
    import java.util.List;
    
    public class JsonUtil {
        static ObjectMapper objectMapper = new ObjectMapper();
        static {
            objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);// 允许pojo中有在json串中不存在的字段
            objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);// 允许有注释
        }
        
        public static <T>T parseObject(InputStream inputStream, Class<T> tClass)  {
            Reader reader = new InputStreamReader(inputStream);
            try {
                return objectMapper.readValue(reader, tClass);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        public static <T>T parseObject(String json,Class<T> tClass){
            try {
                return objectMapper.readValue(json,tClass);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        public static String toJsonString(Object object){
            try {
                return objectMapper.writeValueAsString(object);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        public static <T>List<T> parseList(String json,Class<T> tClass) {
            JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, tClass);
            try {
                List<T> list  = objectMapper.readValue(json, javaType);
                return list;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
    }
  • 相关阅读:
    Python获取秒级时间戳与毫秒级时间戳
    时间戳与时间类型转化(秒级时间戳)
    linux压缩和解压缩命令
    对于Python中@property的理解和使用
    探索性测试方法
    Linux 中 grep 命令的 12 个实践例子
    在 Linux 启动或重启时执行命令与脚本
    亲测的orabbix监控Oracle过程
    find 使用搜集
    Centos7.3-mysql5.7复制安装过程
  • 原文地址:https://www.cnblogs.com/dongma/p/14030978.html
Copyright © 2011-2022 走看看