zoukankan      html  css  js  c++  java
  • JacksonUtils.java

    package com.vcredit.framework.utils;

    import java.io.IOException;
    import java.util.List;

    import com.fasterxml.jackson.core.JsonParseException;
    import com.fasterxml.jackson.core.JsonProcessingException;
    import com.fasterxml.jackson.core.type.TypeReference;
    import com.fasterxml.jackson.databind.JsonMappingException;
    import com.fasterxml.jackson.databind.ObjectMapper;

    /**
    * Jackson JSON 工具类
    */
    public class JacksonUtils {
    private static final ObjectMapper mapper = new ObjectMapper();

    /**
    * 获取Mapper 可以使用工具类没定义的方法
    */
    public static ObjectMapper getMapper() {
    return mapper;
    }

    /**
    * 转换JSON为对象
    *
    * @param json
    * @param cls
    * @return T
    * @throws JsonParseException
    * @throws JsonMappingException
    * @throws IOException
    */
    public static <T> T convertObj(String json, Class<T> cls) throws JsonParseException, JsonMappingException,
    IOException {
    return mapper.readValue(json, cls);
    }

    /**
    * 抓换Json为对象List
    *
    * @param json
    * @param cls
    * @return List<T>
    * @throws JsonParseException
    * @throws JsonMappingException
    * @throws IOException
    */
    public static <T> List<T> convertList(String json, Class<T> cls) throws JsonParseException, JsonMappingException,
    IOException {
    return mapper.readValue(json, new TypeReference<List<T>>() {
    });
    }

    /**
    * 转化JSON
    *
    * @param t
    * @return String
    * @throws JsonProcessingException
    */
    public static <T> String toJson(T t) throws JsonProcessingException {
    return mapper.writeValueAsString(t);
    }

    }

  • 相关阅读:
    Discuz利用百度ping把我们网站自动提交到百度
    正则表达式速查表1
    thinkphp 新浪新闻采集代码演示
    php采集一网站的精美图片
    百度知道的php爬虫
    新浪新闻采集程序
    mysql pid文件丢失解决办法
    js位运算-按位非
    mysql表损坏解决方案
    h5新API之WebStorage解决页面数据通信问题
  • 原文地址:https://www.cnblogs.com/muliu/p/6145349.html
Copyright © 2011-2022 走看看