Json工具類----Jackson
程式碼部分
public class JacksonUtils {
private final static ObjectMapper obejectmapper=new ObjectMapper();
private JacksonUtils(){
}
private static ObjectMapper getInstance(){
return obejectmapper;
}
/**
* javaBean、列表陣列轉換為json字串
*/
public static String obj2json(Object obj) throws Exception {
return obejectmapper.writeValueAsString(obj);
}
/**
* javaBean、列表陣列轉換為json字串,忽略空值
*/
public static String obj2jsonIgnoreNull(Object obj) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper.writeValueAsString(obj);
}
/**
* json 轉JavaBean
*/
public static <T> T json2pojo(String jsonString, Class<T> clazz) throws Exception {
obejectmapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
return obejectmapper.readValue(jsonString, clazz);
}
/**
* json字串轉換為map
*/
public static <T> Map<String, Object> json2map(String jsonString) throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper.readValue(jsonString, Map.class);
}
/**
* json字串轉換為map
*/
public static <T> Map<String, T> json2map(String jsonString, Class<T> clazz) throws Exception {
Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>) obejectmapper.readValue(jsonString, new TypeReference<Map<String, T>>() {
});
Map<String, T> result = new HashMap<String, T>();
for (Map.Entry<String, Map<String, Object>> entry : map.entrySet()) {
result.put(entry.getKey(), map2pojo(entry.getValue(), clazz));
}
return result;
}
/**
* 深度轉換json成map
*
* @param json
* @return
*/
public static Map<String, Object> json2mapDeeply(String json) throws Exception {
return json2MapRecursion(json, obejectmapper);
}
/**
* 把json解析成list,如果list內部的元素存在jsonString,繼續解析
*
* @param json
* @param mapper 解析工具
* @return
* @throws Exception
*/
private static List<Object> json2ListRecursion(String json, ObjectMapper mapper) throws Exception {
if (json == null) {
return null;
}
List<Object> list = mapper.readValue(json, List.class);
for (Object obj : list) {
if (obj != null && obj instanceof String) {
String str = (String) obj;
if (str.startsWith("[")) {
obj = json2ListRecursion(str, mapper);
} else if (obj.toString().startsWith("{")) {
obj = json2MapRecursion(str, mapper);
}
}
}
return list;
}
/**
* 把json解析成map,如果map內部的value存在jsonString,繼續解析
*
* @param json
* @param mapper
* @return
* @throws Exception
*/
private static Map<String, Object> json2MapRecursion(String json, ObjectMapper mapper) throws Exception {
if (json == null) {
return null;
}
Map<String, Object> map = mapper.readValue(json, Map.class);
for (Map.Entry<String, Object> entry : map.entrySet()) {
Object obj = entry.getValue();
if (obj != null && obj instanceof String) {
String str = ((String) obj);
if (str.startsWith("[")) {
List<?> list = json2ListRecursion(str, mapper);
map.put(entry.getKey(), list);
} else if (str.startsWith("{")) {
Map<String, Object> mapRecursion = json2MapRecursion(str, mapper);
map.put(entry.getKey(), mapRecursion);
}
}
}
return map;
}
/**
* 與javaBean json陣列字串轉換為列表
*/
public static <T> List<T> json2list(String jsonArrayStr, Class<T> clazz) throws Exception {
JavaType javaType = getCollectionType(ArrayList.class, clazz);
List<T> lst = (List<T>) obejectmapper.readValue(jsonArrayStr, javaType);
return lst;
}
/**
* 獲取泛型的Collection Type
*
* @param collectionClass 泛型的Collection
* @param elementClasses 元素類
* @return JavaType Java型別
* @since 1.0
*/
public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
return obejectmapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
/**
* map 轉JavaBean
*/
public static <T> T map2pojo(Map map, Class<T> clazz) {
return obejectmapper.convertValue(map, clazz);
}
/**
* map 轉json
*
* @param map
* @return
*/
public static String mapToJson(Map map) {
try {
return obejectmapper.writeValueAsString(map);
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
/**
* map 轉JavaBean
*/
public static <T> T obj2pojo(Object obj, Class<T> clazz) {
return obejectmapper.convertValue(obj, clazz);
}
}
相關文章
- 常用Json工具類JSON
- XML轉化為json工具類XMLJSON
- JSON解析器之Gson、FastJson、JacksonJSONAST
- Java讀取Json檔案工具類JavaJSON
- 終極CRUD-3-用Jackson解析jsonJSON
- 一文搞定Jackson解析JSON資料JSON
- Json反序列化物件通用工具類JSON物件
- 從json-lib轉成jackson的遇到的問題JSON
- 1. 初識Jackson -- 世界上最好的JSON庫JSON
- Spring統一返回Json工具類,帶分頁資訊SpringJSON
- 基於Spring Security Role過濾Jackson JSON輸出內容SpringJSON
- Jackson 解析json資料之忽略解析欄位註解@JsonIgnorePropertiesJSON
- 2. 媽呀,Jackson原來是這樣寫JSON的JSON
- json 組裝工具JSON
- jackson學習之十(終篇):springboot整合(配置類)Spring Boot
- 類轉json的基類實現JSON
- 工具類-字串工具類字串
- 轉載:Spring整合JSON報錯:java.lang.ClassNotFoundException: org.codehaus.jackson.JsonProcessingExceptionSpringJSONJavaException
- 7. Jackson用樹模型處理JSON是必備技能,不信你看模型JSON
- 3. 懂了這些,方敢在簡歷上說會用Jackson寫JSONJSON
- Jackson JSON包在從物件對映到Json字串過程的迴圈依賴問題-分析與解決JSON物件字串
- @JsonCreator自定義反序列化函式-JSON框架Jackson精解第5篇JSON函式框架
- jackson、fastjson、kryo、protostuff等序列化工具效能對比ASTJSON
- 【淺度渣文】Jackson之jackson-annotations
- 【淺度渣文】Jackson之jackson-databind
- URL及日期等特殊資料格式處理-JSON框架Jackson精解第2篇JSON框架
- 自寫Json轉換工具JSON
- 線上JSON轉BigQuery工具JSON
- C#中JSON轉換類C#JSON
- 工具類
- Caused by: com.fasterxml.jackson.databind.JsonMappingException: Incompatible Jackson version: 2.7.3ASTXMLJSONAPPException
- 開發工具-scala處理json格式利器-json4sJSON
- 在Flutter開發過程中快速生成json解析模板類的工具 | 掘金技術徵文FlutterJSON
- 屬性序列化自定義與字母表排序-JSON框架Jackson精解第3篇排序JSON框架
- java工具類之編碼轉換工具類Java
- Java —— 集合工具類(Collections 類)Java
- 字串值提取工具-09-java 執行 json 解析, json-path字串JavaJSON
- Jackson - 淺析@JsonIncludeJSON