老版本的Jackson使用的包名為org.codehaus.jackson
,而新版本使用的是com.fasterxml.jackson
。
Jackson主要包含了3個模塊:
- jackson-core
- jackson-annotations
- jackson-databind
其中,jackson-annotations依賴於jackson-core,jackson-databind又依賴於jackson-annotations。
Jackson有三種方式處理Json:
- 使用底層的基於Stream的方式對Json的每一個小的組成部分進行控制
- 使用Tree Model,通過JsonNode處理單個Json節點
- 使用databind模塊,直接對Java對象進行序列化和反序列化
通常來說,我們在日常開發中使用的是第3種方式,有時為了簡便也會使用第2種方式,比如你要從一個很大的Json對象中只讀取那么一兩個字段的時候,采用databind方式顯得有些重,JsonNode反而更簡單。
package founder.util; import java.io.IOException; import java.util.ArrayList; import java.util.List; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; /** * @ClassName: JsonUtils * @author hanwl * @date 2019年01月22日 * @Description: TODO */ public class JsonUtils { private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); /** * Json格式的字符串向JavaBean轉換,傳入空串將返回null * @param strBody Json格式的字符串 * @param c 目標JavaBean類型 * @return JavaBean對象 * @throws JsonParseException * @throws JsonMappingException * @throws IOException */ public static <T> T json2Object(String strBody, Class<T> c) throws JsonParseException, JsonMappingException, IOException{ if (strBody == null || "".equals(strBody)) { return null; } else { return OBJECT_MAPPER.readValue(strBody, c); } } /** * Json格式的字符串向JavaBean轉換,傳入空串將返回null * @param strBody Json格式的字符串 * @param c 目標JavaBean類型 * @return JavaBean對象, 如果解析失敗返回 null */ public static <T> T decodeJson(String strBody, Class<T> c) { if (strBody == null || "".equals(strBody)) { return null; } else { try { return OBJECT_MAPPER.readValue(strBody, c); } catch (IOException e) { e.printStackTrace(); return null; } } } /** * * @param strBody * @param c * @return * @throws JsonParseException * @throws JsonMappingException * @throws IOException */ public static Object json2ComplexObject(String strBody) throws JsonParseException, JsonMappingException, IOException{ if (strBody == null || "".equals(strBody)) { return null; } else { // 每個屬性的實際類型是string return OBJECT_MAPPER.readValue(strBody, Object.class); } } /** * Json格式的字符串向JavaBean List集合轉換,傳入空串將返回null * @param strBody * @param c * @return * @throws JsonParseException * @throws JsonMappingException * @throws IOException */ @SuppressWarnings("unchecked") public static <T> List<T> json2ObjectList(String strBody,Class<T> c) throws JsonParseException, JsonMappingException, IOException{ if (strBody == null || "".equals(strBody)) { return null; } else { JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametricType(ArrayList.class, c); return (List<T>) OBJECT_MAPPER.readValue(strBody, javaType); } } /** * Json格式的字符串向JavaBean List集合轉換,傳入空串將返回null * @param strBody * @param c * @return 對象列表,解析失敗返回 null */ @SuppressWarnings("unchecked") public static <T> List<T> decodeJsonToList(String strBody,Class<T> c) { if (strBody == null || "".equals(strBody)) { return null; } else { JavaType javaType = OBJECT_MAPPER.getTypeFactory().constructParametricType(ArrayList.class, c); try { return (List<T>) OBJECT_MAPPER.readValue(strBody, javaType); } catch (IOException e) { e.printStackTrace(); return null; } } } /** * Json格式的字符串向List<String>集合轉換,傳入空串將返回null * @param strBody * @return * @throws JsonParseException * @throws JsonMappingException * @throws IOException */ public static List<String> json2List(String strBody) throws JsonParseException, JsonMappingException, IOException{ return json2ObjectList(strBody, String.class); } /** * Object轉為Json格式字符串的方法 * @param o * @return * @throws JsonProcessingException */ public static String object2Json(Object o) throws JsonProcessingException{ return OBJECT_MAPPER.writeValueAsString(o); } /** * Object轉為Json格式字符串的方法 * @param o * @return 對象的json字符串,如果處理過程中出錯,返回null */ public static String encodeObject(Object o) { try { return OBJECT_MAPPER.writeValueAsString(o); } catch (JsonProcessingException e) { e.printStackTrace(); return null; } } }