From 9486766c806fe1d9e082b2fd02ea1cc558f1b443 Mon Sep 17 00:00:00 2001 From: 无关风月 <443237572@qq.com> Date: 星期四, 08 五月 2025 09:21:57 +0800 Subject: [PATCH] bug修改 --- cloud-server-management/src/main/java/com/dsh/course/util/JsonUtil.java | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 157 insertions(+), 0 deletions(-) diff --git a/cloud-server-management/src/main/java/com/dsh/course/util/JsonUtil.java b/cloud-server-management/src/main/java/com/dsh/course/util/JsonUtil.java new file mode 100644 index 0000000..e7c30a0 --- /dev/null +++ b/cloud-server-management/src/main/java/com/dsh/course/util/JsonUtil.java @@ -0,0 +1,157 @@ +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 { + + /** + * <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() { + + } + + /** + * 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字符串。 + * <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数据转换成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; + } + + 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; + } + +} -- Gitblit v1.7.1