public class JsonUtil { private final static ObjectMapper objectMapper = new ObjectMapper(); private JsonUtil() { } public static ObjectMapper getInstance() { return objectMapper; } /**只能轉一級泛型如T<A,B...>*/ public static <T> T toCollection(String jsonStr, Class<?> parametrized, Class<?>... parameterClasses) throws IOException { JavaType javaType = objectMapper.getTypeFactory().constructParametricType(parametrized,parameterClasses); return objectMapper.readValue(jsonStr, javaType); } /** * javaBean,list,array convert to json string */ public static String obj2json(Object obj) throws Exception { return objectMapper.writeValueAsString(obj); } /** * json string convert to javaBean */ public static <T> T json2pojo(String jsonStr, Class<T> clazz) throws Exception { return objectMapper.readValue(jsonStr, clazz); } /** * json string convert to map */ public static <T> Map<String, Object> json2map(String jsonStr) throws Exception { return objectMapper.readValue(jsonStr, Map.class); } /** * json string convert to map with javaBean */ public static <T> Map<String, T> json2map(String jsonStr, Class<T> clazz) throws Exception { Map<String, Map<String, Object>> map = objectMapper.readValue(jsonStr, 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 array string convert to list with javaBean */ public static <T> List<T> json2list(String jsonArrayStr, Class<T> clazz) throws Exception { List<Map<String, Object>> list = objectMapper.readValue(jsonArrayStr, new TypeReference<List<T>>() { }); List<T> result = new ArrayList<T>(); for (Map<String, Object> map : list) { result.add(map2pojo(map, clazz)); } return result; } /** * json string 轉複雜多級的泛型類能夠用這個方法 */ public static <T> T json2TypeReferenceObj(String jsonStr, Class<T> clazz) throws IOException { TypeReference ref = new TypeReference<T>(){}; return objectMapper.readValue(jsonStr, ref); } /** * map convert to javaBean */ public static <T> T map2pojo(Map map, Class<T> clazz) { return objectMapper.convertValue(map, clazz); } public static <T> T readFromStream(InputStream inputStream, Class<?> parametrized, Class<?>... parameterClasses) throws IOException { JavaType javaType = objectMapper.getTypeFactory().constructParametricType(parametrized,parameterClasses); return objectMapper.readValue(inputStream,javaType); } }
注意寫類的getter setter時候的問題:java
jackson默認的字段屬性發現規則以下:全部被public修飾的字段->全部被public修飾的getter->全部被public修飾的setterjson
若是屬性是private 就讀不到了app
Android Studio自動生成的getter setter會出問題的一個點是boolean類型,若是我把變量名定爲isXXX,自動生成的getter setter方法名是沒有get set字眼的,jackson就不能識別了,解決辦法就是本身手動把isXX變量的getter setter方法補全get set字眼。code
對於json string字段多,java 對象的變量少的狀況,能夠有兩種處理方式,對象
1.統一處理,配置不要報錯:get
ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
2.單獨每一個class處理:input
@JsonIgnoreProperties(ignoreUnknown = true) public class Foo{ }