自己封裝的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 <T>
* @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;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/312105.html
標籤:其他
上一篇:一、Java語言的開放環境
下一篇:弱網測驗工具的使用
