您的位置:首页 > Web前端 > JavaScript

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

2016-01-07 15:37 525 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  gson 对象 long jsonEncode