JLChen
2020-06-04 9c2506577fc035855f8e23ac8b3f8fcab8c09eb5
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
package com.hdl.sdk.hdl_core.Util.TransformUtil;
 
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
 
import java.util.List;
 
public class JsonUtil {
 
    public static String toJson(Object object) {
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setPrettyPrinting();
        Gson gson = gsonBuilder.create();
        return gson.toJson(object);
    }
    /**
     *解析字符串字典
     */
    public static <T> T parseJsonWithGson(String json, Class<T> type) {
 
        T result  = new Gson().fromJson(json, type);
        return result;
    }
 
    /**
     * 解析数组
     */
    public static <T> List<T> parseArrayJsonWithGson(String json, Class<T> type) {
        List<T> list = new Gson().fromJson(json, new TypeToken<List<T>>(){}.getType());
        return list;
    }
 
    /**
     *json判断
     */
    public static boolean isJson(String content) {
        JsonElement jsonElement;
        try {
            jsonElement = new JsonParser().parse(content);
        } catch (Exception e) {
            return false;
        }
        if (jsonElement == null) {
            return false;
        }
        if (!jsonElement.isJsonObject()) {
            return false;
        }
        return true;
    }
}