Gson之string 转 map<String,JsonObject>

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;


public class GsonUtil {

    private GsonUtil() {
    }

    private static Gson gson = new Gson();

    public static Gson getInstance() {
        return gson;
    }

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

    public static  T fromJson(String json, Class clazz) {
        try {
            return gson.fromJson(json, clazz);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public static HashMap toGsonMap(String jsonStr) {
        HashMap res = null;
        try {
            res = gson.fromJson(jsonStr, new TypeToken>() {
            }.getType());
        } catch (Exception e) {

        }
        return res;
    }
}

你可能感兴趣的:(gson,java,开发语言)