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{ }