Gson 的深度使用

3 篇文章 0 订阅

一直在使用 google 的 Gson ,一般都是简单用法。今天有空找了找文章,研究了一下深入的使用。

参考文章如下:

完全理解Gson(1):简单入门:http://www.importnew.com/16630.html

完全理解Gson(2):Gson序列化:http://www.importnew.com/16638.html

完全理解Gson(3):Gson反序列化:http://www.importnew.com/16786.html

 

其实上面的3篇连续的文章都是翻译于:  http://www.javacreed.com/

简单入门:http://www.javacreed.com/simple-gson-example/

Gson序列化:http://www.javacreed.com/gson-serialiser-example/

Gson反序列化:http://www.javacreed.com/gson-deserialiser-example/

Gson 注解的使用:http://www.javacreed.com/gson-annotations-example/

这里的网址上面还有其他关于 Gson 的文章:http://www.javacreed.com/tag/gson/

 

其他参考资料:

Gson 官网:https://github.com/google/gson

Gson 官网的 UserGuide:https://github.com/google/gson/blob/master/UserGuide.md (里面有各种序列化和反序列化的例子)

 -----------------------

操作 List:

Type listType = new TypeToken<ArrayList<YourClass>>(){}.getType();

List<YourClass> yourClassList = new Gson().fromJson(jsonArray, listType);

 

    /**
     *
     * @return Gson实例
     * @author xuepeng
     * @CreateTime 2017/11/29 10:59:20
     * @Description 构建Gson实例
     *              将json里面时间字段存储为long类型的时间转换为java的Date类型。
     */
    private Gson buildGson() {
        Gson gson = new GsonBuilder().registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
            @Override
            public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
                    throws JsonParseException {
                return new Date(json.getAsJsonPrimitive().getAsLong());
            }
        }).create();
        return gson;
    }

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值