您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 项目/工程管理 > Google 开源项目 Gson教程
1.简单的处理list和mapJava代码1.Gsongson=newGson();2.ListtestList=newArrayList();3.testList.add(first);4.testList.add(second);5.StringlistToJson=gson.toJson(testList);6.System.out.println(listToJson);7.//prints[first,second]8.9.MaptestMap=newHashMap();10.testMap.put(id,id.first);11.testMap.put(name,name.second);12.StringmapToJson=gson.toJson(testMap);13.System.out.println(mapToJson);14.//prints{id:id.first,name:name.second}Gsongson=newGson();ListtestList=newArrayList();testList.add(first);testList.add(second);StringlistToJson=gson.toJson(testList);System.out.println(listToJson);//prints[first,second]MaptestMap=newHashMap();testMap.put(id,id.first);testMap.put(name,name.second);StringmapToJson=gson.toJson(testMap);System.out.println(mapToJson);//prints{id:id.first,name:name.second}2.处理带泛型的集合Java代码1.ListTestBeantestBeanList=newArrayListTestBean();2.TestBeantestBean=newTestBean();3.testBean.setId(id);4.testBean.setName(name);5.testBeanList.add(testBean);ListTestBeantestBeanList=newArrayListTestBean();TestBeantestBean=newTestBean();testBean.setId(id);testBean.setName(name);testBeanList.add(testBean);Java代码1.java.lang.reflect.Typetype=newcom.google.gson.reflect.TypeTokenListTestBean(){2.}.getType();3.StringbeanListToJson=gson.toJson(testBeanList,type);4.System.out.println(beanListToJson);5.//prints[{id:id,name:name}]6.7.ListTestBeantestBeanListFromJson=gson.fromJson(beanListToJson,type);8.System.out.println(testBeanListFromJson);9.//prints[TestBean@1ea5671[id=id,name=name,birthday=null]]java.lang.reflect.Typetype=newcom.google.gson.reflect.TypeTokenListTestBean(){}.getType();StringbeanListToJson=gson.toJson(testBeanList,type);System.out.println(beanListToJson);//prints[{id:id,name:name}]ListTestBeantestBeanListFromJson=gson.fromJson(beanListToJson,type);System.out.println(testBeanListFromJson);//prints[TestBean@1ea5671[id=id,name=name,birthday=null]]map等其他集合类型同上3.Date类型转化先写工具类Java代码1.importjava.lang.reflect.Type;2.3.importcom.google.gson.JsonDeserializationContext;4.importcom.google.gson.JsonDeserializer;5.importcom.google.gson.JsonElement;6.importcom.google.gson.JsonParseException;7.8.publicclassUtilDateDeserializerimplementsJsonDeserializerjava.util.Date{9.10.@Override11.publicjava.util.Datedeserialize(JsonElementjson,TypetypeOfT,JsonDeserializationContextcontext)12.throwsJsonParseException{13.returnnewjava.util.Date(json.getAsJsonPrimitive().getAsLong());14.}15.}importjava.lang.reflect.Type;importcom.google.gson.JsonDeserializationContext;importcom.google.gson.JsonDeserializer;importcom.google.gson.JsonElement;importcom.google.gson.JsonParseException;publicclassUtilDateDeserializerimplementsJsonDeserializerjava.util.Date{@Overridepublicjava.util.Datedeserialize(JsonElementjson,TypetypeOfT,JsonDeserializationContextcontext)throwsJsonParseException{returnnewjava.util.Date(json.getAsJsonPrimitive().getAsLong());}}Java代码1.importjava.lang.reflect.Type;2.3.importcom.google.gson.JsonElement;4.importcom.google.gson.JsonPrimitive;5.importcom.google.gson.JsonSerializationContext;6.importcom.google.gson.JsonSerializer;7.8.publicclassUtilDateSerializerimplementsJsonSerializerjava.util.Date{9.10.@Override11.publicJsonElementserialize(java.util.Datesrc,TypetypeOfSrc,12.JsonSerializationContextcontext){13.returnnewJsonPrimitive(src.getTime());14.}15.16.}importjava.lang.reflect.Type;importcom.google.gson.JsonElement;importcom.google.gson.JsonPrimitive;importcom.google.gson.JsonSerializationContext;importcom.google.gson.JsonSerializer;publicclassUtilDateSerializerimplementsJsonSerializerjava.util.Date{@OverridepublicJsonElementserialize(java.util.Datesrc,TypetypeOfSrc,JsonSerializationContextcontext){returnnewJsonPrimitive(src.getTime());}}Java代码1./**2.*序列化方法3.*@parambean4.*@paramtype5.*@return6.*/7.publicstaticStringbean2json(Objectbean,Typetype){8.Gsongson=newGsonBuilder().registerTypeAdapter(java.util.Date.class,newUtilDateSerializer())9..setDateFormat(DateFormat.LONG).create();10.returngson.toJson(bean);11.}12.13./**14.*反序列化方法15.*@paramjson16.*@paramtype17.*@return18.*/19.publicstaticTTjson2bean(Stringjson,Typetype){20.Gsongson=newGsonBuilder().registerTypeAdapter(java.util.Date.class,newUtilDateDeserializer())21..setDateFormat(DateFormat.LONG).create();22.returngson.fromJson(json,type);23.}/***序列化方法*@parambean*@paramtype*@return*/publicstaticStringbean2json(Objectbean,Typetype){Gsongson=newGsonBuilder().registerTypeAdapter(java.util.Date.class,newUtilDateSerializer()).setDateFormat(DateFormat.LONG).create();returngson.toJson(bean);}/***反序列化方法*@paramjson*@paramtype*@return*/publicstaticTTjson2bean(Stringjson,Typetype){Gsongson=newGsonBuilder().registerTypeAdapter(java.util.Date.class,newUtilDateDeserializer()).setDateFormat(DateFormat.LONG).create();returngson.fromJson(json,type);}现在开始测试Java代码1.ListTestBeantestBeanList=newArrayListTestBean();2.TestBeantestBean=newTestBean();3.testBean.setId(id);4.testBean.setName(name);5.testBean.setBirthday(newjava.util.Date());6.testBeanList.add(testBean);
本文标题:Google 开源项目 Gson教程
链接地址:https://www.777doc.com/doc-752965 .html