道路环境监测node后端和java后台代码
wangrong
2021-11-24 ac1a5f4542431c28b5865acbeb81985cefdbc2a8
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
package com.example.demo.utils;
 
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
public class JsonUtils {
 
    /**
     * JSON 转 POJO
     */
    public static <T> T getObject(String pojo, Class<T> tclass) {
        try {
            return JSONObject.parseObject(pojo, tclass);
        } catch (Exception e) {
//            log.error(tclass + "转 JSON 失败");
        }
        return null;
    }
 
    /**
     * POJO 转 JSON
     */
    public static <T> String getJson(T tResponse){
        String pojo = JSONObject.toJSONString(tResponse, SerializerFeature.WriteNullStringAsEmpty);
        return pojo;
    }
 
    /**
     * List<T> 转 json 保存到数据库
     */
    public static <T> String listToJson(List<T> ts) {
        String jsons = JSON.toJSONString(ts,SerializerFeature.WriteNullStringAsEmpty);
        return jsons;
    }
 
    /**
     * json 转 List<T>
     */
    public static <T> List<T> jsonToList(String jsonString, Class<T> clazz) {
        @SuppressWarnings("unchecked")
        List<T> ts = (List<T>) JSONArray.parseArray(jsonString, clazz);
        return ts;
    }
 
    public static boolean isJsonStr(String jsonStr) {
        Object object = JSON.parse(jsonStr);
        boolean ret = true;
        if (object instanceof JSONObject) {
        } else if (object instanceof JSONArray) {
        } else {
            ret = false;
        }
        return ret;
    }
 
    public static String parseJsonStr(String jsonStr) {
        Object object = JSON.parse(jsonStr);
        StringBuffer retStr = new StringBuffer();
        if (object instanceof JSONObject) {
            JSONObject jsonObject = (JSONObject) object;
            for (Map.Entry<String, Object> entry : jsonObject.entrySet()) {
                retStr.append('&').append(entry.getKey()).append('=').append(entry.getValue());
            }
        }
        return retStr.toString();
    }
 
 
 
}