月球大数据地理空间分析展示平台-【后端】-月球后台服务
1
13693261870
2024-11-13 eb40365c9cffb2269fd3cbd31b050c33455bc84a
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
package com.moon.server.helper;
 
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
 
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
 
@SuppressWarnings("ALL")
public class JsonHelper {
    @SuppressWarnings("unchecked")
    public static LinkedHashMap<String, Object> json2Map(String json) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true).configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        return mapper.readValue(json, LinkedHashMap.class);
    }
 
    public static String map2Json(Object obj) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true).configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        return mapper.writeValueAsString(obj);
    }
 
    @SuppressWarnings("unchecked")
    public static List<LinkedHashMap<String, Object>> json2ListMap(String json) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true).configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        return mapper.readValue(json, List.class);
    }
 
    public static <T> T json2Object(String json, Class<T> valueType) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true).configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        return mapper.readValue(json, valueType);
    }
 
    public static <T> T obj2Vo(Object obj, Class<T> valueType) {
        return JSONObject.parseObject(JSONObject.toJSONString(obj), valueType);
    }
}