package com.bpa.scalage.helper;

import com.google.gson.Gson;

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

public class Json<T>{
    /**
     * 对象转化json
     */
    public String objectToJson(Class<T> clazz,T pointKMBean) {
        Gson gson = new Gson();
        String jsonStr = gson.toJson(pointKMBean, clazz);
        return jsonStr;
    }
    /**
     * json转化对象
     */
    public T jsonToobject(Class<T> clazz,String objectJson) {
        Gson gson = new Gson();
        T pointKMBean = gson.fromJson(objectJson, clazz);
        return pointKMBean;
    }
    /**
     * 获取List
     */
    public static <T> List<T> jsonToListobject(Class<T> clazz, String strJson) {
        List<T> datalist = new ArrayList<>();
        if (null == strJson) {
            return datalist;
        }
        Gson gson = new Gson();
        //        datalist = gson.fromJson(strJson, new TypeToken<List<T>>() {}.getType());
        //改为下面的方法，clazz传入实际想要解析出来的类
        Type listType = com.google.gson.internal.$Gson$Types.newParameterizedTypeWithOwner(null, ArrayList.class, clazz);
        datalist = gson.fromJson(strJson, listType);
        return datalist;
    }
}
