Jackson处理对象和json之间转换的工具类

自己封装的jackson处理对象和json之间互相转化的工具类

public class JsonUtil {

    /**
     * 把对象转成json字符串
     * @param obj
     * @return
     */
    public static String convertJsonString(Object obj){
        ObjectMapper objectMapper = new ObjectMapper();
        String str = null;
        try{
            str = objectMapper.writeValueAsString(obj);
        }catch (Exception e){
            e.printStackTrace();
        }
        return str;
    }

    /**
     * 把json转成对象
     * @param json
     * @param objClass
     * @param 
     * @return
     */
    public static <T> T convertObj(String json, Class<T> objClass){
        ObjectMapper objectMapper = new ObjectMapper();
        T t = null;
        try {
            t = objectMapper.readValue(json, objClass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return t;
    }
}

你可能感兴趣的:(SpringBoot,json,jackson,jackson工具类,对象和json转化工具类)