Gson 的深度使用

一直在使用 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;
    }

猜你喜欢

转载自kanpiaoxue.iteye.com/blog/2390720