com.qcloud.vod.util.JacksonUtil Maven / Gradle / Ivy
package com.qcloud.vod.util;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* json工具类
* @author jianguoxu
* @time 2017/9/4 20:55
*/
public class JacksonUtil {
public static ObjectMapper objectMapper;
/**
* 使用泛型方法,把json字符串转换为相应的JavaBean对象
* @param jsonStr
* @param valueType
* @param
* @return
*/
public static T readValue(String jsonStr, Class valueType) {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
}
try {
return objectMapper.readValue(jsonStr, valueType);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 把JavaBean转换为json字符串
* @param object
* @return
*/
public static String toJSon(Object object) {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
}
try {
return objectMapper.writeValueAsString(object);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy