content: package com.jingewenku.abrahamcaijin.commonutil;
  
  import com.google.gson.Gson;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  import org.json.JSONTokener;
  
  import java.lang.reflect.Array;
  import java.math.BigDecimal;
  import java.math.BigInteger;
  import java.util.*;
  
  /**
   * @Description:主要功能:Json工具类(需要依赖Gson 2.0以上)
   * @Prject: CommonUtilLibrary
   * @Package: com.jingewenku.abrahamcaijin.commonutil
   * @author: AbrahamCaiJin
   * @date: 2017年06月06日 16:16
   * @Copyright: 个人版权所有
   * @Company:
   * @version: 1.0.0
   */
  
  public class JsonUtils {
  
      /**
       * 对象转json
       * @param obj
       * @return
       */
      public static String toJson(Object obj) {
          Gson gson = new Gson();
          return gson.toJson(obj);
      }
  
      /**
       * json转对象
       * @param str
       * @param type
       * @param <T>
       * @return
       */
      public static <T> T fromJson(String str, Class<T> type) {
          Gson gson = new Gson();
          return gson.fromJson(str, type);
      }
  
      /**
       * Map转为JSONObject
       * @param data
       * @return
       */
      public static JSONObject map2Json(Map<?, ?> data) {
          JSONObject object = new JSONObject();
  
          for (Map.Entry<?, ?> entry : data.entrySet()) {
              String key = (String) entry.getKey();
              if (key == null) {
                  throw new NullPointerException("key == null");
              }
              try {
                  object.put(key, wrap(entry.getValue()));
              } catch (JSONException e) {
                  e.printStackTrace();
              }
          }
  
          return object;
      }
  
      /**
       * 集合转换为JSONArray
       * @param data
       * @return
       */
      public static JSONArray collection2Json(Collection<?> data) {
          JSONArray jsonArray = new JSONArray();
          if (data != null) {
              for (Object aData : data) {
                  jsonArray.put(wrap(aData));
              }
          }
          return jsonArray;
      }
  
      /**
       * Object对象转换为JSONArray
       * @param data
       * @return
       * @throws JSONException
       */
      public static JSONArray object2Json(Object data) throws JSONException {
          if (!data.getClass().isArray()) {
              throw new JSONException("Not a primitive data: " + data.getClass());
          }
          final int length = Array.getLength(data);
          JSONArray jsonArray = new JSONArray();
          for (int i = 0; i < length; ++i) {
              jsonArray.put(wrap(Array.get(data, i)));
          }
  
          return jsonArray;
      }
  
      private static Object wrap(Object o) {
          if (o == null) {
              return null;
          }
          if (o instanceof JSONArray || o instanceof JSONObject) {
              return o;
          }
          try {
              if (o instanceof Collection) {
                  return collection2Json((Collection<?>) o);
              } else if (o.getClass().isArray()) {
                  return object2Json(o);
              }
              if (o instanceof Map) {
                  return map2Json((Map<?, ?>) o);
              }
  
              if (o instanceof Boolean || o instanceof Byte || o instanceof Character || o instanceof Double || o instanceof Float || o instanceof Integer || o instanceof Long
                  || o instanceof Short || o instanceof String) {
                  return o;
              }
              if (o.getClass().getPackage().getName().startsWith("java.")) {
                  return o.toString();
              }
          } catch (Exception ignored) {
          }
          return null;
      }
  
      /**
       * json字符串生成JSONObject对象
       * @param json
       * @return
       */
      public static JSONObject string2JSONObject(String json) {
          JSONObject jsonObject = null;
          try {
              JSONTokener jsonParser = new JSONTokener(json);
              jsonObject = (JSONObject) jsonParser.nextValue();
          } catch (Exception e) {
              e.printStackTrace();
          }
          return jsonObject;
      }
  
      /**
       * 对象转换为Json
       * @param obj
       * @return
       */
      public static String object2json(Object obj) {
          StringBuilder json = new StringBuilder();
          if (obj == null) {
              json.append("\"\"");
          } else if (obj instanceof String || obj instanceof Integer
              || obj instanceof Float || obj instanceof Boolean
              || obj instanceof Short || obj instanceof Double
              || obj instanceof Long || obj instanceof BigDecimal
              || obj instanceof BigInteger || obj instanceof Byte) {
              json.append("\"").append(string2json(obj.toString())).append("\"");
          } else if (obj instanceof Object[]) {
              json.append(array2json((Object[]) obj));
          } else if (obj instanceof List) {
              json.append(list2json((List<?>) obj));
          } else if (obj instanceof Map) {
              json.append(map2json((Map<?, ?>) obj));
          } else if (obj instanceof Set) {
              json.append(set2json((Set<?>) obj));
          }
          return json.toString();
      }
  
      /**
       * List集合转换为Json
       * @param list
       * @return
       */
      public static String list2json(List<?> list) {
          StringBuilder json = new StringBuilder();
          json.append("[");
          if (list != null && list.size() > 0) {
              for (Object obj : list) {
                  json.append(object2json(obj));
                  json.append(",");
              }
              json.setCharAt(json.length() - 1, ']');
          } else {
              json.append("]");
          }
          return json.toString();
      }
  
      /**
       * 对象数组转换为Json
       * @param array
       * @return
       */
      public static String array2json(Object[] array) {
          StringBuilder json = new StringBuilder();
          json.append("[");
          if (array != null && array.length > 0) {
              for (Object obj : array) {
                  json.append(object2json(obj));
                  json.append(",");
              }
              json.setCharAt(json.length() - 1, ']');
          } else {
              json.append("]");
          }
          return json.toString();
      }
  
      /**
       * Map集合转换为Json
       * @param map
       * @return
       */
      public static String map2json(Map<?, ?> map) {
          StringBuilder json = new StringBuilder();
          json.append("{");
          if (map != null && map.size() > 0) {
              for (Object key : map.keySet()) {
                  json.append(object2json(key));
                  json.append(":");
                  json.append(object2json(map.get(key)));
                  json.append(",");
              }
              json.setCharAt(json.length() - 1, '}');
          } else {
              json.append("}");
          }
          return json.toString();
      }
  
      /**
       * Set集合转为Json
       * @param set
       * @return
       */
      public static String set2json(Set<?> set) {
          StringBuilder json = new StringBuilder();
          json.append("[");
          if (set != null && set.size() > 0) {
              for (Object obj : set) {
                  json.append(object2json(obj));
                  json.append(",");
              }
              json.setCharAt(json.length() - 1, ']');
          } else {
              json.append("]");
          }
          return json.toString();
      }
  
      /**
       * 字符串转换为Json
       * @param s
       * @return
       */
      public static String string2json(String s) {
          if (s == null)
              return "";
          StringBuilder sb = new StringBuilder();
          for (int i = 0; i < s.length(); i++) {
              char ch = s.charAt(i);
              switch (ch) {
                  case '"':
                      sb.append("\\\"");
                      break;
                  case '\\':
                      sb.append("\\\\");
                      break;
                  case '\b':
                      sb.append("\\b");
                      break;
                  case '\f':
                      sb.append("\\f");
                      break;
                  case '\n':
                      sb.append("\\n");
                      break;
                  case '\r':
                      sb.append("\\r");
                      break;
                  case '\t':
                      sb.append("\\t");
                      break;
                  case '/':
                      sb.append("\\/");
                      break;
                  default:
                      if (ch >= '\u0000' && ch <= '\u001F') {
                          String ss = Integer.toHexString(ch);
                          sb.append("\\u");
                          for (int k = 0; k < 4 - ss.length(); k++) {
                              sb.append('0');
                          }
                          sb.append(ss.toUpperCase());
                      } else {
                          sb.append(ch);
                      }
              }
          }
          return sb.toString();
      }
  
      /**
       *字符数组转List集合
       * @param array String[] array = new String[] {"zhu", "wen", "tao"};
       * @return  List
       */
      public static List<String> array2List(String[] array){
          // String数组转List集合
          List<String> mlist = Arrays.asList(array);
          // 输出List集合
          for (int i = 0; i < mlist.size(); i++) {
              AppLogMessageMgr.d(JsonUtils.class.getSimpleName() + "---array2List-->" + mlist.get(i));
          }
          return mlist;
      }
  
      /**
       *
       * @param list ArrayList<String> list = new ArrayList<>();
       * @return String[]
       */
      public static String[] list2Array(List<String> list){
          String[] array = list.toArray(new String[list.size()]);
          System.out.println("列表长度为："+list.size());
          System.out.println("将列表转化为数组：");
          for (int i = 0; i < array.length; i++) {
              System.out.print(array[i]+"  ");
          }
          return  array;
      }
  
  }
  