不灭的焱

革命尚未成功,同志仍须努力下载JDK17

作者:Albert.Wen  添加时间:2018-06-28 20:02:48  修改时间:2024-04-19 16:56:56  分类:Java基础  编辑

你真的会用Gson吗?Gson使用指南(一)

你真的会用Gson吗?Gson使用指南(二)

你真的会用Gson吗?Gson使用指南(三)

你真的会用Gson吗?Gson使用指南(四)

 

搞定Gson泛型封装

约定

1、涉及到的 json 格式

// data 为 object 的情况
{"code":0,"msg":"success","data":{}}
// data 为 array 的情况
{"code":0,"msg":"success","data":[]}

2、假定第一种的对应的Java类型为 Result<XXX> ,第二种为 Result<List<XXX>>

代码汇总

utils/gson/ParameterizedTypeImpl.java

package utils.gson;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public class ParameterizedTypeImpl implements ParameterizedType {
    private final Class raw;
    private final Type[] args;

    public ParameterizedTypeImpl(Class raw, Type[] args) {
        this.raw = raw;
        this.args = args != null ? args : new Type[0];
    }

    @Override
    public Type[] getActualTypeArguments() {
        return args;
    }

    @Override
    public Type getRawType() {
        return raw;
    }

    @Override
    public Type getOwnerType() {
        return null;
    }
}

utils/gson/Result.java

package utils.gson;

public class Result<T> {
    private int code = 0;
    private String msg = "";
    private String debug_msg = "";
    private T data = null;

    public static Result Create() {
        return new Result();
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getDebug_msg() {
        return debug_msg;
    }

    public void setDebug_msg(String debug_msg) {
        this.debug_msg = debug_msg;
    }
}

utils/gson/GsonUtils.java

package utils.gson;

import com.google.gson.Gson;

import java.lang.reflect.Type;
import java.util.List;

public class GsonUtils {
    private static Gson GSON = new Gson();
    public static <T> Result<T> fromJsonObject(String reader, Class<T> clazz) {
        Type type = new ParameterizedTypeImpl(Result.class, new Class[]{clazz});
        return GSON.fromJson(reader, type);
    }


    public static <T> Result<List<T>> fromJsonArray(String reader, Class<T> clazz) {
        // 生成List<T> 中的 List<T> 
        Type listType = new ParameterizedTypeImpl(List.class, new Class[]{clazz});

        // 根据List<T>生成完整的Result<List<T>> 
        Type type = new ParameterizedTypeImpl(Result.class, new Type[]{listType});
        return GSON.fromJson(reader, type);
    }
}

test/Test.java

public class Test {
    public static void main(String[] args) {
		String str = "{\"code\":0,\"msg\":\"\",\"data\":[{\"ModifyIp\":\"172.16.66.59\",\"ModifyTime\":\"2018-06-14 10:22:59\",\"DeviceGroupUID\":\"12086366-FB5B-E30E-1689-DBA29B663C4B\",\"DeviceUID\":\"63D6DA46-148F-0DBB-9634-22B0D93078B6\",\"DeviceGroupName\":\"深圳市xxx科技有限公司\",\"Description\":\"\",\"IsEnabled\":\"1\",\"DelFlag\":\"0\",\"CreateIP\":\"172.16.66.59\",\"CreateTime\":\"2018-06-14 10:22:59\",\"UID\":\"95B591CA-24F2-B7A1-4E3C-25031CC3F5A8\",\"ApplicationID\":\"6AEC4B81-A046-E5E0-52FD-4B3AA185F593\",\"DeviceType\":\"GX\",\"DeviceSN\":\"972291000061\",\"DeviceTimeZone\":\"E8\",\"CarSeriesCode\":null,\"DeviceStatus\":\"2\",\"CarType\":null,\"DeviceVersion\":null,\"DeviceKey\":\"3d003600055136343836313600000000\",\"DeviceInfo\":\"\",\"DeviceIMEI\":null,\"SIMSN\":null,\"AppKey\":null,\"Delisting\":null,\"ServiceStartTime\":\"2018-06-14 11:10:25\",\"ServiceEndTime\":\"0000-00-00 00:00:00\"}]}";
        
        try {
            Result<List<Device>> r3 = GsonUtils.fromJsonArray(str, Device.class);
            List<Device> devList = r3.getData();
            for (Device device : devList) {
                System.out.println(device.getDeviceGroupName());
            }
        } catch (Exception e) {
            System.out.println(str);
        }
	}
}