【常用工具类】解析工具类

来源:互联网 发布:java stringbuffer类 编辑:程序博客网 时间:2024/06/13 23:06
    public static String jsonEncode(Object object) {        Gson gson = new Gson();        return gson.toJson(object);    }    public static Object jsonDecode(String jsonString, Type type) {        if (jsonString == null) {            return null;        }        Gson gson = new Gson();        Object object = null;        try {            object = gson.fromJson(jsonString, type);        } catch (Exception e) {            Util.log(e.getMessage());            e.printStackTrace();        }        return object;    }    public static Map<Object, Object> jsonDecode(String jsonString) {        if (jsonString == null) {            return null;        }        Gson gson = new Gson();        Map<Object, Object> map = null;        try {            map = gson.fromJson(jsonString, new TypeToken<Map<Object, Object>>() {            }.getType());        } catch (Exception e) {            Util.log(e.getMessage());        }        return map;    }    public static int parseInt(Object obj) {        try {            return (int) Double.parseDouble("" + obj);        } catch (Exception e) {        }        return 0;    }    public static long parseLong(Object obj) {        try {            return (long) Double.parseDouble("" + obj);        } catch (Exception e) {        }        return 0;    }    public static float parseFloat(Object obj) {        try {            return Float.parseFloat("" + obj);        } catch (Exception e) {        }        return 0;    }
0 0