package com.dsh.course.util; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.serializer.SerializerFeature; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.text.SimpleDateFormat; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /** * * 返回json数据封装工具类 * * @ClassName: JsonUtil * @author: yuds * @date: 2019-09-20 09:19:27 * @version: V1.0.0 */ public class JsonUtil { /** *
  • 将对象转化为json输出 *
  • QuoteFieldNames ———-输出key时是否使用双引号,默认为true *
  • WriteMapNullValue ——–是否输出值为null的字段,默认为false *
  • WriteNullNumberAsZero —-数值字段如果为null,输出为0,而非null *
  • WriteNullListAsEmpty —–List字段如果为null,输出为[],而非null *
  • WriteNullStringAsEmpty —字符类型字段如果为null,输出为”“,而非null *
  • WriteNullBooleanAsFalse –Boolean字段如果为null,输出为false,而非null *
  • DisableCircularReferenceDetect 禁止循环引用检测 */ private static SerializerFeature[] serializerFeaturess = { SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullNumberAsZero, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullBooleanAsFalse, SerializerFeature.DisableCircularReferenceDetect, }; private JsonUtil() { } /** * 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); } // 定义jackson对象 private static final ObjectMapper MAPPER = new ObjectMapper(); /** * 将对象转换成json字符串。 *

    * Title: pojoToJson *

    *

    * Description: *

    * * @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 jsonToPojo(String jsonData, Class beanType) { try { T t = MAPPER.readValue(jsonData, beanType); return t; } catch (Exception e) { e.printStackTrace(); } return null; } /** * 将json数据转换成pojo对象list *

    * Title: jsonToList *

    *

    * Description: *

    * * @param jsonData * @param beanType * @return */ public static List jsonToList(String jsonData, Class beanType) { JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType); try { List list = MAPPER.readValue(jsonData, javaType); return list; } catch (Exception e) { e.printStackTrace(); } return null; } public static JsonNode jsonToNode(String jsonData) { try { return MAPPER.readTree(jsonData); } catch (Exception ex) { ex.printStackTrace(); } return null; } public static Map jsonToMap(String json) { JsonNode nodes = jsonToNode(json); Iterator> fields = nodes.fields(); Map map = new HashMap<>(); while (fields.hasNext()) { Map.Entry next = fields.next(); String value = next.getValue().asText(""); map.put(next.getKey(), value); } return map; } }