whycxzp
2025-03-26 427ec9e696494cedcde2d9c58e9565d1db6117aa
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
package com.whyc.util;
 
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
 
import java.lang.reflect.Type;
 
public class JsonUtil {
 
    /**
     *
     * @param dataType    需要解析的日期的格式如:"yyyy-MM-dd HH:mm:ss"
     * @return    得到对应的gson对象
     */
    public static Gson getGson(String dataType){
        return new GsonBuilder().setDateFormat(dataType).create();
    }
 
    /**
     * 获取默认的gson对象
     * @return
     */
    public static Gson getGson(){
        return new Gson();
    }
 
    public static <T> T getObject(String jsonstring, Type listtype){
        Gson gson=new Gson();
        T t=null;
        try {
            t=gson.fromJson(jsonstring, listtype);
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
        }
        return t;
    }
 
}