分享一个Gson解析Json字符串的工具类

public class GsonUtils {

    public static String createGsonString(Object object) {
        Gson gson = new Gson();
        String gsonString = gson.toJson(object);
        return gsonString;
    }

    public static  T changeGsonToBean(String gsonString, Class cls) {
        Gson gson = new Gson();
        T t = gson.fromJson(gsonString, cls);
        return t;
    }

    public static  List changeGsonToList(String gsonString, Class cls) {
        Gson gson = new Gson();
        List list = gson.fromJson(gsonString, new TypeToken>() {
    }.getType());
        return list;
    }


    public static  List> changeGsonToListMaps(
        String gsonString) {
        List> list = null;
        Gson gson = new Gson();
        list = gson.fromJson(gsonString, new TypeToken>>() {
    }.getType());
        return list;
    }

    public static  Map changeGsonToMaps(String gsonString) {
        Map map = null;
        Gson gson = new Gson();
        map = gson.fromJson(gsonString, new TypeToken>() {
    }.getType());
        return map;
    }

}

你可能感兴趣的:(分享一个Gson解析Json字符串的工具类)