JsonUtils.java 11 KB

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