自定义类型适配器的Gson工具类
来源:互联网 发布:adobe pdf 电脑软件 编辑:程序博客网 时间:2024/06/05 23:08
在android开发以及javaEE的开发过程中,我们需要解析json,下面我为大家提供一个已经封装好的工具类,在使用过程中,完全不需要再对gson作处理。一行代码即可以实现转换
public final class JsonUtil { private JsonUtil(){} /** * 对象转换成json字符串 * @param obj * @return */ public static String toJson(Object obj) { Gson gson = new GsonBuilder().registerTypeAdapter(Integer.class, new IntegerDefault0Adapter()) .registerTypeAdapter(int.class, new IntegerDefault0Adapter()) .registerTypeAdapter(Double.class, new DoubleDefault0Adapter()) .registerTypeAdapter(double.class, new DoubleDefault0Adapter()) .registerTypeAdapter(Long.class, new LongDefault0Adapter()) .registerTypeAdapter(long.class, new LongDefault0Adapter()) .create(); return gson.toJson(obj); } /** * json字符串转成对象 * @param str * @param type * @return */ public static <T> T fromJson(String str, Type type) { Gson gson = new GsonBuilder().registerTypeAdapter(Integer.class, new IntegerDefault0Adapter()) .registerTypeAdapter(int.class, new IntegerDefault0Adapter()) .registerTypeAdapter(Double.class, new DoubleDefault0Adapter()) .registerTypeAdapter(double.class, new DoubleDefault0Adapter()) .registerTypeAdapter(Long.class, new LongDefault0Adapter()) .registerTypeAdapter(long.class, new LongDefault0Adapter()) .create(); return gson.fromJson(str, type); } /** * json字符串转成对象 * @param str * @param type * @return */ public static <T> T fromJson(String str, Class<T> type) { Gson gson = new GsonBuilder().registerTypeAdapter(Integer.class, new IntegerDefault0Adapter()) .registerTypeAdapter(int.class, new IntegerDefault0Adapter()) .registerTypeAdapter(Double.class, new DoubleDefault0Adapter()) .registerTypeAdapter(double.class, new DoubleDefault0Adapter()) .registerTypeAdapter(Long.class, new LongDefault0Adapter()) .registerTypeAdapter(long.class, new LongDefault0Adapter()) .create(); return gson.fromJson(str, type); }}
在这里,我们使用了IntegerDefault0Adapter,DoubleDefault0Adapter,LongDefault0Adapter自定义类型适配器,用来解决解析过程中的类型转换异常
IntegerDefault0Adapter.java
public class IntegerDefault0Adapter implements JsonSerializer<Integer>, JsonDeserializer<Integer> { @Override public Integer deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { if (json.getAsString().equals("") || json.getAsString().equals("null")) {//定义为int类型,如果后台返回""或者null,则返回0 return 0; } } catch (Exception ignore) { } try { return json.getAsInt(); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } } @Override public JsonElement serialize(Integer src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src); }}
DoubleDefault0Adapter.java
public class DoubleDefault0Adapter implements JsonSerializer<Double>, JsonDeserializer<Double> { @Override public Double deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { if (json.getAsString().equals("") || json.getAsString().equals("null")) {//定义为double类型,如果后台返回""或者null,则返回0.00 return 0.00; } } catch (Exception ignore) { } try { return json.getAsDouble(); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } } @Override public JsonElement serialize(Double src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src); }}
LongDefault0Adapter.java
public class LongDefault0Adapter implements JsonSerializer<Long>, JsonDeserializer<Long> { @Override public Long deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { try { if (json.getAsString().equals("") || json.getAsString().equals("null")) {//定义为long类型,如果后台返回""或者null,则返回0 return 0l; } } catch (Exception ignore) { } try { return json.getAsLong(); } catch (NumberFormatException e) { throw new JsonSyntaxException(e); } } @Override public JsonElement serialize(Long src, Type typeOfSrc, JsonSerializationContext context) { return new JsonPrimitive(src); }}
阅读全文
0 0
- 自定义类型适配器的Gson工具类
- 自定义GSON类型适配器
- Gson 基础教程 —— 自定义类型适配器(TypeAdapter)
- gson自定义适配器
- Gson自定义适配器处理特殊解析异常
- JsonDeserializer——Gson自定义解析类型错误的字段
- Google Gson的一个便捷工具类
- 实用的json工具类gson
- 使用Gson的Json转换工具类
- Gson工具的抽取
- 31会话界面的适配器中的工具类---使用Spannable自定义TextView
- Gson工具类
- Gson解析工具类
- 谷歌Gson工具类
- Gson工具类
- Gson 转换工具类
- Gson工具类
- Gson工具类
- Spark入门实战系列
- 动态改变表单form的路径
- Idea 安装 antlr 插件后 ANTLR Preview 怎么用的
- 快来看看阿里巴巴的常用面试题
- 敦煌api 获取access_token和用户信息
- 自定义类型适配器的Gson工具类
- ubuntu source.list源不能用的情况下怎么做
- hive优化:让一个MR做更多的事情
- NOIP2015 子串 dp
- Android检查更新库CheckUpdateLibrary使用教程(一)
- javascript 浏览器之间跳转传递参数(不支持中文字符)
- 如何在.Net的C#中制作DLL文件,读取ini文件
- 详述 Java 中的别名现象 .
- 查看linux服务器的系统信息