JsonUtils.java 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package com.example.demo.util;
  2. import com.fasterxml.jackson.databind.JavaType;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.google.gson.Gson;
  5. import com.google.gson.GsonBuilder;
  6. import com.google.gson.JsonElement;
  7. import com.google.gson.JsonSyntaxException;
  8. import com.google.gson.reflect.TypeToken;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import java.lang.reflect.InvocationTargetException;
  12. import java.lang.reflect.Method;
  13. import java.lang.reflect.Modifier;
  14. import java.lang.reflect.Type;
  15. import java.util.ArrayList;
  16. import java.util.HashMap;
  17. import java.util.List;
  18. import java.util.Map;
  19. /**
  20. * json解析的工具类
  21. */
  22. public class JsonUtils {
  23. // 定义jackson对象
  24. private static final ObjectMapper MAPPER = new ObjectMapper();
  25. public static Logger log = LoggerFactory.getLogger(JsonUtils.class);
  26. /**
  27. * 把对象转换为json数据
  28. *
  29. * @param obj
  30. * @return 2018年5月7日 下午5:27:16
  31. */
  32. public static String objectToJson(Object obj) {
  33. Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
  34. try {
  35. String json = gson.toJson(obj);
  36. return json;
  37. } catch (Exception e) {
  38. e.printStackTrace();
  39. }
  40. return null;
  41. }
  42. /**
  43. * 将Object转换成Map
  44. *
  45. * @param obj
  46. * @return
  47. */
  48. public static Map<String, Object> objectToMap(Object obj) {
  49. try {
  50. Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
  51. String json = gson.toJson(obj);
  52. return jsonToMap(json);
  53. } catch (Exception e) {
  54. e.printStackTrace();
  55. }
  56. return null;
  57. }
  58. /**
  59. * 将Object类型的map转换成String类型
  60. *
  61. * @param map
  62. * @return
  63. */
  64. public static Map<String, String> mapToMap(Map<String, Object> map) {
  65. Map<String, String> returnMap = new HashMap<>();
  66. for (String key : map.keySet()) {
  67. returnMap.put(key, String.valueOf(map.get(key)));
  68. }
  69. return returnMap;
  70. }
  71. /**
  72. * 任意类型转换成Map
  73. * @return
  74. */
  75. public static Map<String, String> object2Map(Object obj) {
  76. Map<String, String> hashMap = new HashMap();
  77. try {
  78. Class c = obj.getClass();
  79. Method m[] = c.getDeclaredMethods();
  80. for (int i = 0; i < m.length; i++) {
  81. if (m[i].getName().indexOf("get")==0) {
  82. // 得到Map的key
  83. String suffixKey = m[i].getName().substring(4);
  84. String prefixKey = m[i].getName().substring(3,4).toLowerCase();
  85. hashMap.put(prefixKey + suffixKey, String.valueOf(m[i].invoke(obj, new Object[0])));
  86. }
  87. }
  88. } catch (Throwable e) {
  89. log.error(e.getMessage());
  90. }
  91. return hashMap;
  92. }
  93. /**
  94. * 把json字符串转化为对象
  95. *
  96. * @param jsonString
  97. * @param clazz
  98. * @return
  99. */
  100. public static Object jsonToObject(String jsonString, Class<?> clazz) {
  101. Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
  102. Object obj = null;
  103. try {
  104. obj = gson.fromJson(jsonString, clazz);
  105. } catch (JsonSyntaxException e) {
  106. e.printStackTrace();
  107. }
  108. return obj;
  109. }
  110. /**
  111. * josn转arrayList
  112. *
  113. * @param jsonArray
  114. * @return
  115. */
  116. public static ArrayList<?> jsonArrayToArrayList(String jsonArray) {
  117. Gson gson = new GsonBuilder()
  118. .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
  119. .setDateFormat("yyyy-MM-dd HH:mm:ss")
  120. .serializeNulls()
  121. .create();
  122. ArrayList<?> list = null;
  123. try {
  124. Type listType = new TypeToken<ArrayList<?>>() {
  125. }.getType();
  126. list = gson.fromJson(jsonArray, listType);
  127. } catch (JsonSyntaxException e) {
  128. e.printStackTrace();
  129. }
  130. return list;
  131. }
  132. /**
  133. * JSON 转 ArrayList
  134. */
  135. public static ArrayList<?> jsonArrayToArrayList(String jsonArray, Class<?> clazz) {
  136. Gson gson = new GsonBuilder()
  137. .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
  138. .setDateFormat("yyyy-MM-dd HH:mm:ss")
  139. .serializeNulls()
  140. .create();
  141. ArrayList<?> list = null;
  142. try {
  143. list = (ArrayList<?>) gson.fromJson(jsonArray, clazz);
  144. } catch (JsonSyntaxException e) {
  145. e.printStackTrace();
  146. }
  147. return list;
  148. }
  149. /**
  150. * 把json转换为map类型的数据
  151. *
  152. * @param json
  153. * @return
  154. */
  155. public static Map<String, Object> jsonToMap(String json) {
  156. Gson gson = new GsonBuilder()
  157. .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
  158. .setDateFormat("yyyy-MM-dd HH:mm:ss")
  159. .serializeNulls()
  160. .create();
  161. Map<String, Object> map = null;
  162. try {
  163. Type type = new TypeToken<Map<String, Object>>() {
  164. }.getType();
  165. map = gson.fromJson(json, type);
  166. } catch (JsonSyntaxException e) {
  167. e.printStackTrace();
  168. }
  169. return map;
  170. }
  171. /**
  172. * 将Json转换成Map<String, ?>
  173. *
  174. * @param json
  175. * @param clazz
  176. * @return
  177. */
  178. public static Map<String, ?> jsonToMap(String json, Class<?> clazz) {
  179. Gson gson = new GsonBuilder()
  180. .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
  181. .setDateFormat("yyyy-MM-dd HH:mm:ss")
  182. .serializeNulls()
  183. .create();
  184. Map<String, ?> map = null;
  185. try {
  186. Type type = new TypeToken<Map<String, ?>>() {
  187. }.getType();
  188. map = gson.fromJson(json, type);
  189. } catch (JsonSyntaxException e) {
  190. e.printStackTrace();
  191. }
  192. return map;
  193. }
  194. /**
  195. * 将map转换成pojo
  196. *
  197. * @param map
  198. * @param beanType
  199. * @param <T>
  200. * @return
  201. */
  202. public static <T> T mapToPojo(Map<String, Object> map, Class<T> beanType) {
  203. Gson gson = new GsonBuilder()
  204. .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
  205. .setDateFormat("yyyy-MM-dd HH:mm:ss")
  206. .serializeNulls()
  207. .create();
  208. JsonElement jsonElement = gson.toJsonTree(map);
  209. T pojo = gson.fromJson(jsonElement, beanType);
  210. return pojo;
  211. }
  212. /**
  213. * 将json结果集转化为对象
  214. *
  215. * @param jsonData
  216. * @param beanType
  217. * @param <T>
  218. * @return
  219. */
  220. public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
  221. try {
  222. T t = MAPPER.readValue(jsonData, beanType);
  223. return t;
  224. } catch (Exception e) {
  225. e.printStackTrace();
  226. }
  227. return null;
  228. }
  229. /**
  230. * 将json数据转换成pojo对象list
  231. *
  232. * @param jsonData
  233. * @param beanType
  234. * @return
  235. */
  236. public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
  237. JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
  238. try {
  239. List<T> list = MAPPER.readValue(jsonData, javaType);
  240. return list;
  241. } catch (Exception e) {
  242. e.printStackTrace();
  243. }
  244. return null;
  245. }
  246. /**
  247. * 将任意pojo转化成map
  248. *
  249. * @param t pojo对象
  250. * @return
  251. */
  252. public static <T> Map<String, Object> pojoToMap(T t) {
  253. Map<String, Object> result = new HashMap<String, Object>();
  254. Method[] methods = t.getClass().getMethods();
  255. try {
  256. for (Method method : methods) {
  257. Class<?>[] paramClass = method.getParameterTypes();
  258. // 如果方法带参数,则跳过
  259. if (paramClass.length > 0) {
  260. continue;
  261. }
  262. String methodName = method.getName();
  263. if (methodName.startsWith("get")) {
  264. Object value = method.invoke(t);
  265. result.put(methodName, value);
  266. }
  267. }
  268. } catch (IllegalArgumentException e) {
  269. e.printStackTrace();
  270. } catch (IllegalAccessException e) {
  271. e.printStackTrace();
  272. } catch (InvocationTargetException e) {
  273. e.printStackTrace();
  274. } catch (SecurityException e) {
  275. e.printStackTrace();
  276. }
  277. return result;
  278. }
  279. }