2014.08.05 ——— android Gson JsonDeserializer
16lz
2021-01-25
2014.08.05 ——— android Gson JsonDeserializer
Gson用起来很方便,但是有的时候 需要对某一个字段进行特殊处理的时候,就比较麻烦,这个时候 我们需要JsonDeserializer
需求:
服务器返回的是秒,我需要将其转换为毫秒
解决:
Gson用起来很方便,但是有的时候 需要对某一个字段进行特殊处理的时候,就比较麻烦,这个时候 我们需要JsonDeserializer
需求:
服务器返回的是秒,我需要将其转换为毫秒
解决:
import java.lang.reflect.Type;import com.google.xlgson.JsonDeserializationContext;import com.google.xlgson.JsonDeserializer;import com.google.xlgson.JsonElement;import com.google.xlgson.JsonObject;import com.google.xlgson.JsonParseException;public class FavoriteRecordJsonType implements JsonDeserializer<FavoriteRecordPo>{ @Override public FavoriteRecordPo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { FavoriteRecordPo tInfo = new FavoriteRecordPo(); JsonObject tJson = json.getAsJsonObject(); tInfo.movietitle = tJson.get("movietitle").getAsString(); tInfo.movieid = tJson.get("movieid").getAsLong(); tInfo.movietype = tJson.get("movietype").getAsString(); tInfo.storetime = tJson.get("storetime").getAsLong() * 1000; return tInfo; }}
GsonBuilder builder = new GsonBuilder(); builder.registerTypeAdapter(FavoriteRecordPo.class, new FavoriteRecordJsonType()); gson = builder.create();
更多相关文章
- 万字长文带你了解最常用的开源 Squid 代理服务器
- Nginx系列教程(一)| 手把手教你在Linux环境下搭建Nginx服务
- Nginx系列教程(三)| 一文带你读懂Nginx的负载均衡
- Android(安卓)SDK 更新时连接出现“https://dl-ssl.google.com r
- Android(安卓)Socket实例(2)
- android通讯录数据库表解析和添加,访问通讯录
- Android(安卓)apache httpd 终端上的服务器
- Android: Gallery的adapter中getView方法被执行多次
- android选择图片或拍照图片上传到服务器(包括上传参数)