Fixiaobai
2023-11-07 22594e714c57c9e243fe9973515ea9467d71c2db
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
package com.chinaztt.mes.common.util;
 
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
 
import java.text.SimpleDateFormat;
 
/**
 * @Author 张宾
 */
public class JsonUtil {
 
    private static final ObjectMapper JSON_MAPPER = new ObjectMapper().disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    static {
        JSON_MAPPER.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        JSON_MAPPER.registerModule(new JavaTimeModule());
    }
 
    /**
     * json转换对应实体
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T jsonToPojo(String json, Class<T> clazz){
        T t=null;
        try {
            t=JSON_MAPPER.readValue(json,clazz);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return t;
    }
 
    /**
     * 实体转换成json字符
     * @param t
     * @param <T>
     * @return
     */
    public static <T> String jsonToString(T t){
        String result=null;
        try {
            result=JSON_MAPPER.writeValueAsString(t);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return result;
    }
 
}