peternameyakj
2024-07-30 f23b33bbb9eaff76cac5b69e3b793fc7910fb0fa
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
/*
 * Copyright (c) 2017. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * www.hnapay.com
 */
 
package project.web.api;
 
import com.alibaba.fastjson.JSONArray;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
 
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.io.BufferedReader;
/**
 * JSON操作工具类
 *
 */
public class JsonUtils {
    /**
     * 将JSON串转为Map
     *
     * @param json
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public static Map<String, Object> jsonToMap(String json) throws Exception {
        if (StringUtils.isBlank(json)) {
            return null;
        }
 
        Map<String, Object> model = new HashMap<String, Object>();
        ObjectMapper mapper = new ObjectMapper();
        System.out.println("************:"+json);
        model = mapper.readValue(json, Map.class);
 
        return model;
    }
 
    /**
     * 将对象解析为json串
     *
     * @param obj
     * @return
     * @throws JsonGenerationException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static String toJson(Object obj) throws JsonGenerationException, JsonMappingException, IOException {
        ObjectMapper mapper = new ObjectMapper();
 
        String json = mapper.writeValueAsString(obj);
 
        return json;
    }
 
    /**
     * JSON数组转LIST
     * @param json
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> List<T> jsonArrayToList(String json,Class<T> clazz){
        return JSONArray.parseArray(json,clazz);
 
    }
 
}