无关风月
2024-07-11 eb6b6dbb35a9f029e0b7d269773685c19fd40976
cloud-server-course/src/main/java/com/dsh/course/util/JsonUtil.java
@@ -14,9 +14,8 @@
import java.util.Map;
/**
 *
 * 返回json数据封装工具类
 *
 *
 * @ClassName: JsonUtil
 * @author: yuds
 * @date: 2019-09-20 09:19:27
@@ -24,134 +23,134 @@
 */
public class JsonUtil {
   /**
    * <li>将对象转化为json输出
    * <li>QuoteFieldNames ———-输出key时是否使用双引号,默认为true
    * <li>WriteMapNullValue ——–是否输出值为null的字段,默认为false
    * <li>WriteNullNumberAsZero —-数值字段如果为null,输出为0,而非null
    * <li>WriteNullListAsEmpty —–List字段如果为null,输出为[],而非null
    * <li>WriteNullStringAsEmpty —字符类型字段如果为null,输出为”“,而非null
    * <li>WriteNullBooleanAsFalse –Boolean字段如果为null,输出为false,而非null
    * <li>DisableCircularReferenceDetect 禁止循环引用检测
    */
   private static SerializerFeature[] serializerFeaturess = { SerializerFeature.WriteMapNullValue,
         SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullListAsEmpty,
         SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullBooleanAsFalse,
         SerializerFeature.DisableCircularReferenceDetect, };
    /**
     * <li>将对象转化为json输出
     * <li>QuoteFieldNames ———-输出key时是否使用双引号,默认为true
     * <li>WriteMapNullValue ——–是否输出值为null的字段,默认为false
     * <li>WriteNullNumberAsZero —-数值字段如果为null,输出为0,而非null
     * <li>WriteNullListAsEmpty —–List字段如果为null,输出为[],而非null
     * <li>WriteNullStringAsEmpty —字符类型字段如果为null,输出为”“,而非null
     * <li>WriteNullBooleanAsFalse –Boolean字段如果为null,输出为false,而非null
     * <li>DisableCircularReferenceDetect 禁止循环引用检测
     */
    private static SerializerFeature[] serializerFeaturess = {SerializerFeature.WriteMapNullValue,
            SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullListAsEmpty,
            SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullBooleanAsFalse,
            SerializerFeature.DisableCircularReferenceDetect,};
   private JsonUtil() {
    private JsonUtil() {
   }
    }
   /**
    * fastJson Object转json
    *
    * @param obj
    * @return
    */
   public static String objToJson(Object obj) {
      return JSON.toJSONString(obj, serializerFeaturess);
   }
    /**
     * fastJson Object转json
     *
     * @param obj
     * @return
     */
    public static String objToJson(Object obj) {
        return JSON.toJSONString(obj, serializerFeaturess);
    }
   /**
    * fastJson Object转json
    *
    * @param obj
    * @param dateFormat 时间格式化
    * @return
    */
   public static String objToJson(Object obj, String dateFormat) {
      return JSON.toJSONStringWithDateFormat(obj, dateFormat, serializerFeaturess);
   }
    /**
     * fastJson Object转json
     *
     * @param obj
     * @param dateFormat 时间格式化
     * @return
     */
    public static String objToJson(Object obj, String dateFormat) {
        return JSON.toJSONStringWithDateFormat(obj, dateFormat, serializerFeaturess);
    }
   // 定义jackson对象
   private static final ObjectMapper MAPPER = new ObjectMapper();
    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();
   /**
    * 将对象转换成json字符串。
    * <p>
    * Title: pojoToJson
    * </p>
    * <p>
    * Description:
    * </p>
    *
    * @param data
    * @return
    */
   public static String objectToJson(Object data) {
      try {
         SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm");
         MAPPER.setDateFormat(fmt);
         String string = MAPPER.writeValueAsString(data);
         return string;
      } catch (JsonProcessingException e) {
         e.printStackTrace();
      }
      return null;
   }
    /**
     * 将对象转换成json字符串。
     * <p>
     * Title: pojoToJson
     * </p>
     * <p>
     * Description:
     * </p>
     *
     * @param data
     * @return
     */
    public static String objectToJson(Object data) {
        try {
            SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            MAPPER.setDateFormat(fmt);
            String string = MAPPER.writeValueAsString(data);
            return string;
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return null;
    }
   /**
    * 将json结果集转化为对象
    *
    * @param jsonData json数据
    * @param beanType    对象中的object类型
    * @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结果集转化为对象
     *
     * @param jsonData json数据
     * @param beanType 对象中的object类型
     * @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
    * <p>
    * Title: jsonToList
    * </p>
    * <p>
    * Description:
    * </p>
    *
    * @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();
      }
    /**
     * 将json数据转换成pojo对象list
     * <p>
     * Title: jsonToList
     * </p>
     * <p>
     * Description:
     * </p>
     *
     * @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;
   }
        return null;
    }
   public static JsonNode jsonToNode(String jsonData) {
      try {
         return MAPPER.readTree(jsonData);
      } catch (Exception ex) {
         ex.printStackTrace();
      }
      return null;
   }
    public static JsonNode jsonToNode(String jsonData) {
        try {
            return MAPPER.readTree(jsonData);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
   public static Map<String, String> jsonToMap(String json) {
      JsonNode nodes = jsonToNode(json);
      Iterator<Map.Entry<String, JsonNode>> fields = nodes.fields();
      Map<String, String> map = new HashMap<>();
      while (fields.hasNext()) {
         Map.Entry<String, JsonNode> next = fields.next();
         String value = next.getValue().asText("");
         map.put(next.getKey(), value);
      }
      return map;
   }
    public static Map<String, String> jsonToMap(String json) {
        JsonNode nodes = jsonToNode(json);
        Iterator<Map.Entry<String, JsonNode>> fields = nodes.fields();
        Map<String, String> map = new HashMap<>();
        while (fields.hasNext()) {
            Map.Entry<String, JsonNode> next = fields.next();
            String value = next.getValue().asText("");
            map.put(next.getKey(), value);
        }
        return map;
    }
}