123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- package com.cslg.ppa.common.utils;
- import com.alibaba.fastjson.JSONObject;
- import com.fasterxml.jackson.core.type.TypeReference;
- import com.fasterxml.jackson.databind.DeserializationFeature;
- import com.fasterxml.jackson.databind.JavaType;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.google.gson.Gson;
- import com.google.gson.GsonBuilder;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonSyntaxException;
- import com.google.gson.reflect.TypeToken;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import java.io.IOException;
- import java.lang.reflect.InvocationTargetException;
- import java.lang.reflect.Method;
- import java.lang.reflect.Modifier;
- import java.lang.reflect.Type;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- /**
- * json解析的工具类
- */
- public class JsonUtils {
- // 定义jackson对象
- public static Logger log = LoggerFactory.getLogger(JsonUtils.class);
- private static ObjectMapper MAPPER;
- static {
- MAPPER = new ObjectMapper();
- MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
- }
- /**
- * 把对象转换为json数据
- *
- * @param obj
- * @return 2018年5月7日 下午5:27:16
- */
- public static String objectToJson(Object obj) {
- Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
- try {
- String json = gson.toJson(obj);
- return json;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 将Object转换成Map
- *
- * @param obj
- * @return
- */
- public static Map<String, Object> objectToMap(Object obj) {
- try {
- Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
- String json = gson.toJson(obj);
- return jsonToMap(json);
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 将Object类型的map转换成String类型
- *
- * @param map
- * @return
- */
- public static Map<String, String> mapToMap(Map<String, Object> map) {
- Map<String, String> returnMap = new HashMap<>();
- for (String key : map.keySet()) {
- returnMap.put(key, String.valueOf(map.get(key)));
- }
- return returnMap;
- }
- /**
- * 任意类型转换成Map
- * @return
- */
- public static Map<String, String> object2Map(Object obj) {
- Map<String, String> hashMap = new HashMap();
- try {
- Class c = obj.getClass();
- Method m[] = c.getDeclaredMethods();
- for (int i = 0; i < m.length; i++) {
- if (m[i].getName().indexOf("get")==0) {
- // 得到Map的key
- String suffixKey = m[i].getName().substring(4);
- String prefixKey = m[i].getName().substring(3,4).toLowerCase();
- hashMap.put(prefixKey + suffixKey, String.valueOf(m[i].invoke(obj, new Object[0])));
- }
- }
- } catch (Throwable e) {
- log.error(e.getMessage());
- }
- return hashMap;
- }
- /**
- * 把json字符串转化为对象
- *
- * @param jsonString
- * @param clazz
- * @return
- */
- public static Object jsonToObject(String jsonString, Class<?> clazz) {
- Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
- Object obj = null;
- try {
- obj = gson.fromJson(jsonString, clazz);
- } catch (JsonSyntaxException e) {
- e.printStackTrace();
- }
- return obj;
- }
- /**
- * josn转arrayList
- *
- * @param jsonArray
- * @return
- */
- public static ArrayList<?> jsonArrayToArrayList(String jsonArray) {
- Gson gson = new GsonBuilder()
- .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
- .setDateFormat("yyyy-MM-dd HH:mm:ss")
- .serializeNulls()
- .create();
- ArrayList<?> list = null;
- try {
- Type listType = new TypeToken<ArrayList<?>>() {
- }.getType();
- list = gson.fromJson(jsonArray, listType);
- } catch (JsonSyntaxException e) {
- e.printStackTrace();
- }
- return list;
- }
- /**
- * JSON 转 ArrayList
- */
- public static ArrayList<?> jsonArrayToArrayList(String jsonArray, Class<?> clazz) {
- Gson gson = new GsonBuilder()
- .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
- .setDateFormat("yyyy-MM-dd HH:mm:ss")
- .serializeNulls()
- .create();
- ArrayList<?> list = null;
- try {
- list = (ArrayList<?>) gson.fromJson(jsonArray, clazz);
- } catch (JsonSyntaxException e) {
- e.printStackTrace();
- }
- return list;
- }
- /**
- * 把json转换为map类型的数据
- *
- * @param json
- * @return
- */
- public static Map<String, Object> jsonToMap(String json) {
- Gson gson = new GsonBuilder()
- .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
- .setDateFormat("yyyy-MM-dd HH:mm:ss")
- .serializeNulls()
- .create();
- Map<String, Object> map = null;
- try {
- Type type = new TypeToken<Map<String, Object>>() {
- }.getType();
- map = gson.fromJson(json, type);
- } catch (JsonSyntaxException e) {
- e.printStackTrace();
- }
- return map;
- }
- /**
- * 将Json转换成Map<String, ?>
- *
- * @param json
- * @param clazz
- * @return
- */
- public static Map<String, ?> jsonToMap(String json, Class<?> clazz) {
- Gson gson = new GsonBuilder()
- .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
- .setDateFormat("yyyy-MM-dd HH:mm:ss")
- .serializeNulls()
- .create();
- Map<String, ?> map = null;
- try {
- Type type = new TypeToken<Map<String, ?>>() {
- }.getType();
- map = gson.fromJson(json, type);
- } catch (JsonSyntaxException e) {
- e.printStackTrace();
- }
- return map;
- }
- /**
- * 将map转换成pojo
- *
- * @param map
- * @param beanType
- * @param <T>
- * @return
- */
- public static <T> T mapToPojo(Map<String, Object> map, Class<T> beanType) {
- Gson gson = new GsonBuilder()
- .excludeFieldsWithModifiers(Modifier.FINAL, Modifier.TRANSIENT, Modifier.STATIC)
- .setDateFormat("yyyy-MM-dd HH:mm:ss")
- .serializeNulls()
- .create();
- JsonElement jsonElement = gson.toJsonTree(map);
- T pojo = gson.fromJson(jsonElement, beanType);
- return pojo;
- }
- /**
- * 将json结果集转化为对象
- *
- * @param jsonData
- * @param beanType
- * @param <T>
- * @return
- */
- public static <T> T jsonToPojo(String jsonData, Class<T> beanType) {
- try {
- T t = MAPPER.readValue(jsonData, beanType);
- return t;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 将json数据转换成pojo对象list
- *
- * @param jsonData
- * @param beanType
- * @return
- */
- public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
- JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
- try {
- List<T> list = MAPPER.readValue(jsonData, javaType);
- return list;
- } catch (Exception e) {
- e.printStackTrace();
- }
- return null;
- }
- /**
- * 将任意pojo转化成map
- *
- * @param t pojo对象
- * @return
- */
- public static <T> Map<String, Object> pojoToMap(T t) {
- Map<String, Object> result = new HashMap<String, Object>();
- Method[] methods = t.getClass().getMethods();
- try {
- for (Method method : methods) {
- Class<?>[] paramClass = method.getParameterTypes();
- // 如果方法带参数,则跳过
- if (paramClass.length > 0) {
- continue;
- }
- String methodName = method.getName();
- if (methodName.startsWith("get")) {
- Object value = method.invoke(t);
- result.put(methodName, value);
- }
- }
- } catch (IllegalArgumentException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InvocationTargetException e) {
- e.printStackTrace();
- } catch (SecurityException e) {
- e.printStackTrace();
- }
- return result;
- }
- /**
- * 将二叉树json对象转为sql where后的条件语句
- *liRJ
- * @param jsonObject jsaonObject
- * @return
- */
- public static String reSql(JSONObject jsonObject){
- String str1 = "";
- String str2 = "";
- JSONObject jsonLeft =jsonObject.getJSONObject("left");
- JSONObject jsonRight =jsonObject.getJSONObject("right");
- if(jsonLeft.containsKey("left")){
- str1 = reSql(jsonLeft);
- }
- else{
- str1 = jsonLeft.get("field").toString()+jsonLeft.get("opr").toString()+jsonLeft.get("value").toString();
- }
- if(jsonRight.containsKey("right")){
- str2= reSql( jsonRight);
- }
- else{
- str2 =jsonRight.get("field").toString()+jsonRight.get("opr").toString()+jsonRight.get("value").toString();
- }
- String sql ="("+ str1+") "+ jsonObject.get("logicOpr")+" ("+str2+")";
- return sql;
- }
- /**
- * json转对象
- * @param jsonStr
- * @param clazz
- * @param <T>
- * @return
- */
- public static <T> T jsonToObj(String jsonStr, Class<T> clazz) {
- try {
- return MAPPER.readValue(jsonStr, clazz);
- } catch (IOException e) {
- throw new RuntimeException("json转换出错", e);
- }
- }
- /**
- * json转对象
- * @param jsonStr
- * @param typeReference
- * @param <T>
- * @return
- */
- public static <T> T jsonToObj(String jsonStr, TypeReference<T> typeReference) {
- try {
- return MAPPER.readValue(jsonStr, typeReference);
- } catch (IOException e) {
- throw new RuntimeException("json转换出错", e);
- }
- }
- }
|