gson工具类

方法调用:

1. UtilJson.toJson(list);
2. User us = (User) UtilJson.fromJson(user, User.class);
3. List list = UtilJson.fromJsonType(ids, new TypeToken>() {
        }.getType());
 int[] uids = UtilJson.fromJsonType(ids, newTypeToken(){}.getType());

工具类:

package com.guo.hui.util;


import java.lang.reflect.Type;

import com.google.gson.Gson;

public class UtilJson {

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

    public static  Object fromJson(String json, Class t) {
        Gson gson = new Gson();
        return gson.fromJson(json, t);
    }

    public static  T fromJsonType(String json, Type t) {
        if (json == null) {
            return null;
        }
        Gson gson = new Gson();
        return gson.fromJson(json, t);
    }


}

你可能感兴趣的:(java)