欢迎来到天天文库
浏览记录
ID:52794061
大小:362.55 KB
页数:12页
时间:2020-03-30
《Google_开源项目(Gson)经典完全教程.pdf》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、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.te
2、stMap.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);Sys
3、tem.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.Listte
4、stBeanList=newArrayList();2.TestBeantestBean=newTestBean();3.testBean.setId("id");4.testBean.setName("name");5.testBeanList.add(testBean);ListtestBeanList=newArrayList();TestBeantestBean=newTestBean();testBean.setId("id");testBean.setName("name");testBeanLis
5、t.add(testBean);Java代码1.java.lang.reflect.Typetype=newcom.google.gson.reflect.TypeToken>(){2.}.getType();3.StringbeanListToJson=gson.toJson(testBeanList,type);4.System.out.println(beanListToJson);5.//prints[{"id":"id","name":"name"}]6.7.ListtestBeanListFromJson=g
6、son.fromJson(beanListToJson,type);8.System.out.println(testBeanListFromJson);9.//prints[TestBean@1ea5671[id=id,name=name,birthday=]]java.lang.reflect.Typetype=newcom.google.gson.reflect.TypeToken>(){}.getType();StringbeanListToJson=gson.toJson(testBeanList,type);Syst
7、em.out.println(beanListToJson);//prints[{"id":"id","name":"name"}]ListtestBeanListFromJson=gson.fromJson(beanListToJson,type);System.out.println(testBeanListFromJson);//prints[TestBean@1ea5671[id=id,name=name,birthday=]]map等其他集合类型同上3.Date类型转化先写工具类Java代码1.i
此文档下载收益归作者所有