Import of java.util.ArrayList; Import java.util.List; Import a java.util.Map; Import com.google.gson.Gson; Import com.google.gson.GsonBuilder; Import com.google.gson.JsonArray; Import COM .google.gson.JsonElement; Import com.google.gson.JsonParser; Import com.google.gson.reflect.TypeToken; public class GsonUtil { // do not create an object, you can call directly Gson method. Private static Gson GSON = null ; // determines whether an object exists gson, the object does not exist, create static { iF(GSON == null ) { // GSON Gson new new = (); // When using GsonBuilder mode attribute is empty json output of the key for key string is displayed in the form of a "key": null, new direct out no "key": null the GSON = new GsonBuilder () setDateFormat. ( "the mM-dd-YYYY HH: mm: SS" ) .create (); } } // no parameters private constructor Private GsonUtil () { } / ** * json the object into a format * * @param Object * @return String * / public static String GsonString (Object Object) { String gsonString= null; if (gson != null) { gsonString = gson.toJson(object); } return gsonString; } /** * 将json转成特定的cls的对象 * * @param gsonString * @param cls * @return */ public static <T> T GsonToBean(String gsonString, Class<T> cls) { T t = null; if (gson != null) { //Json incoming objects and object types, the object is transformed into json T = gson.fromJson (gsonString, CLS); } return T; } / ** * json string converted into List * * @param gsonString * @param CLS * @ return * / public static <T> List <T> GsonToList (String gsonString, Class <T> CLS) { List <T> List = null ; IF (! GSON = null ) { // returns parsed according to the generic type specified , TypeToken <List <T >> { }. getType () Get return type list = gson.fromJson(gsonString, new TypeToken<List<T>>() { }.getType()); } return list; } /** * json字符串转成list中有map的 * * @param gsonString * @return */ public static <T> List<Map<String, T>> GsonToListMaps(String gsonString) { List<Map<String, T>> list = null; if (gson != null) { list = gson.fromJson(gsonString, new TypeToken<List<Map<String, T>>>() { }.getType()); } return list; } /** * json字符串转成map的 * * @param gsonString * @return */ public static <T> Map<String, T> GsonToMaps(String gsonString) { Map<String, T> map = null; if (gson != null) { map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() { }.getType()); } return map; } }
public class JacksonUtils { Private Final static ObjectMapper ObjectMapper = new new ObjectMapper (); Private JacksonUtils () { } public static ObjectMapper the getInstance () { return ObjectMapper; } / ** * javaBean, json string array into a list * / public static String obj2json (Object obj) throws Exception { return objectMapper.writeValueAsString (obj); } / ** * javaBean, json list array into a string, ignoring null * / 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 { objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); return objectMapper.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 = objectMapper.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 { returnjson2MapRecursion (json, ObjectMapper); } / ** * json parsed into the list, if the list element is present inside jsonString, continue parsing * * @param json * @param Mapper analysis tool * @return * @throws Exception * / Private static List <Object> json2ListRecursion (String JSON, ObjectMapper Mapper) throws Exception { IF (JSON == null ) { return null ; } List <Object> = mapper.readValue List (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 array and string into a list * / public static <T> List <T> json2list (String jsonArrayStr, Class <T> clazz) throws Exception { The javaType the javaType = getCollectionType (the ArrayList. class , clazz); List <T> LST = (List <T> ) objectMapper.readValue (jsonArrayStr, the javaType); return LST; } / ** * Get generics Type Collection * *@param collectionClass 泛型的Collection * @param elementClasses 元素类 * @return JavaType Java类型 * @since 1.0 */ public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) { return objectMapper.getTypeFactory().constructParametricType(collectionClass, elementClasses); } /** * map 转JavaBean */ public static <T> T map2pojo(Map map, Class<T> clazz) { return objectMapper.convertValue(map, clazz); } /** * map 转json * * @param map * @return */ public static String mapToJson(Map map) { try { return objectMapper.writeValueAsString(map); } catch (Exception e) { e.printStackTrace(); } return ""; } /** * map 转JavaBean */ public static <T> T obj2pojo(Object obj, Class<T> clazz) { return objectMapper.convertValue(obj, clazz); } }
/ ** * FASTJSON tools * @version : 1.0.0 * / public class FastJsonUtils { Private static Final SerializeConfig config; static { config = new new SerializeConfig (); config.put (java.util.Date. Class , new new JSONLibDataFormatSerializer ( )); // use and json-lib date output formats compatible config.put (a java.sql.Date. class , new new JSONLibDataFormatSerializer ()); // use json-lib and output formats compatible date } Private static Final SerializerFeature [] = {SerializerFeature.WriteMapNullValue Features, // output vacant field SerializerFeature.WriteNullListAsEmpty, // List If the field is null, the output is [], not null SerializerFeature.WriteNullNumberAsZero, // numeric field If null, the output is 0, not null SerializerFeature.WriteNullBooleanAsFalse, // Boolean is false if the field is null, the output, rather than the null SerializerFeature.WriteNullStringAsEmpty // character type if the field is null, the output is "" not null }; public static String the toJSONString (Object Object) { return JSON.toJSONString (Object, config, Features); } public static String toJSONNoFeatures(Object object) { return JSON.toJSONString(object, config); } public static Object toBean(String text) { return JSON.parse(text); } public static <T> T toBean(String text, Class<T> clazz) { return JSON.parseObject(text, clazz); } // 转换为数组 public static <T> Object[] toArray(String text) { return toArray(text, null); } // 转换为数组 public static <T> Object[] toArray(String text, Class<T> clazz) { return JSON.parseArray(text, clazz).toArray(); } // 转换为List public static <T> List<T> toList(String text, Class<T> clazz) { return JSON.parseArray(text, clazz); } /** * 将javabean转化为序列化的json字符串 * @param keyvalue * @return */ public static Object beanToJson(KeyValue keyvalue) { String textJson = JSON.toJSONString(keyvalue); Object objectJson =The JSON.parse (textJson); return objectJson; } / ** * string will be converted to serialized json string * @param keyValue * @return * / public static Object textToJson (String text) { Object objectJson = the JSON.parse ( text); return objectJson; } / ** * JSON string into Map * @param S * @return * / public static the Map stringToCollect (string S) { the Map m = JSONObject.parseObject (S); return m; } /** * 将map转化为string * @param m * @return */ public static String collectToString(Map m) { String s = JSONObject.toJSONString(m); return s; } }