puzhibing
2023-11-25 53e7558400dcacecdce70e39ebfe1727740f9296
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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;
    }
 
}