Gson使用三(集合的处理,一对多处理)
一、概述
Map保存的是键值对的形式,Json的格式也是键值对的,所以正常情况下,map跟json之间的转换应当是理所当然的事情。
二、Map参考实例
package com.ickes.json; import java.lang.reflect.Type; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; import com.ickes.json.daomain.User; /** * Gson对Map的处理 * @author Ickes * */ public class MapTest { public static void main(String[] args) { /** * 普通map处理 */ Map<String,String> map = new HashMap<String,String>(); map.put("name","ickes"); map.put("pwd", "123"); Gson gson = new GsonBuilder() .enableComplexMapKeySerialization() .setPrettyPrinting() .setDateFormat("yyyy-MM-dd HH:mm:ss") .create(); //map的序列化 String json=gson.toJson(map); System.out.println("map的序列化:\n"+json); //map的反序列化 Type typeMap = new TypeToken<Map<String,String>>(){}.getType(); map = gson.fromJson(json,typeMap); System.out.println("map的反序列化:"); for (Map.Entry<String,String> entry: map.entrySet()) { System.out.println(entry.getKey()+"-"+entry.getValue()); } /** * /map对象测试 */ Map<String,User> mapUser = new HashMap<String,User>(); User user1 = new User("A001", "xl","xl_123",24,12000F,new Date()); User user2 = new User("A002", "x2","xl_223",24,13000F,new Date()); mapUser.put("user1",user1); mapUser.put("user2",user2); json = gson.toJson(mapUser); System.out.println("Map对象的序列化:\n"+json); //map对象的反序列化 Type typeUser = new TypeToken<Map<String,User>>(){}.getType(); mapUser = gson.fromJson(json, typeUser); System.out.println("map对象的反序列化:"); for (Map.Entry<String,User> entry: mapUser.entrySet()) { System.out.println(entry.getKey()+"-"+entry.getValue()); } /** * 整合测试 */ Map<String,List<User>> mapList = new HashMap<String,List<User>>(); List<User> users = new ArrayList<User>(); users.add(user1); users.add(user2); mapList.put("mapList",users); //序列化 json = gson.toJson(mapList); System.out.println("mapList的序列化:\n"+json); //反序列化 Type typeMapList = new TypeToken<Map<String,List<User>>>(){}.getType(); mapList = gson.fromJson(json, typeMapList); System.out.println("mapList对象的反序列化:"); for (Map.Entry<String,List<User>> entry: mapList.entrySet()) { System.out.println(entry.getKey()+"-"+entry.getValue()); } } }
返回结果:
map的序列化: { "pwd": "123", "name": "ickes" } map的反序列化: pwd-123 name-ickes Map对象的序列化: { "user2": { "id": "A002", "userName": "x2", "userPwd": "xl_223", "age": 24, "price": 13000.0, "birthday": "2015-01-13 16:06:48" }, "user1": { "id": "A001", "userName": "xl", "userPwd": "xl_123", "age": 24, "price": 12000.0, "birthday": "2015-01-13 16:06:48" } } map对象的反序列化: user2-User [id=A002, userName=x2, userPwd=xl_223, age=24, price=13000.0, birthday=Tue Jan 13 16:06:48 CST 2015] user1-User [id=A001, userName=xl, userPwd=xl_123, age=24, price=12000.0, birthday=Tue Jan 13 16:06:48 CST 2015] mapList的序列化: { "mapList": [ { "id": "A001", "userName": "xl", "userPwd": "xl_123", "age": 24, "price": 12000.0, "birthday": "2015-01-13 16:06:48" }, { "id": "A002", "userName": "x2", "userPwd": "xl_223", "age": 24, "price": 13000.0, "birthday": "2015-01-13 16:06:48" } ] } mapList对象的反序列化: mapList-[User [id=A001, userName=xl, userPwd=xl_123, age=24, price=12000.0, birthday=Tue Jan 13 16:06:48 CST 2015], User [id=A002, userName=x2, userPwd=xl_223, age=24, price=13000.0, birthday=Tue Jan 13 16:06:48 CST 2015]]
温馨提示:Gson在序列化Map时,默认情况下,是调用Key的toString方法得到它的JSON字符串的Key,对于简单类型和字符串类型,这没有问题,但是对于复杂数据对象,如果对象没有覆写toString方法,那么默认的toString方法将得到这个对象的Hash地址,通过调用GsonBuilder的enableComplexMapKeySerialization()来启用对Map键(key)的序列化.
三、一对多参考实例
下面实例提供了User(员工)对Dept(部门)一对多的映射关系
User实体类:
package com.ickes.json.daomain; import java.util.Date; /** * @author Ickes */ public class User { private String userName; private String userPwd; private Integer age; private Date birthday; public User(String userName, String userPwd, Integer age, Date birthday) { super(); this.userName = userName; this.userPwd = userPwd; this.age = age; this.birthday = birthday; } get()和set()方法省略.....! @Override public String toString() { return "User [userName=" + userName + ", userPwd=" + userPwd + ", age=" + age + ", birthday=" + birthday + "]"; } }
Dept实体类:
package com.ickes.json.daomain; import java.util.List; public class Dept { private String id; private String deptName; private List<User> users; public Dept(String id, String deptName) { super(); this.id = id; this.deptName = deptName; } get()和set()方法省略......! @Override public String toString() { return "Dept [id=" + id + ", deptName=" + deptName + ", users=" + users + "]"; } }
测试实体类:
package com.ickes.json.daomain; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.google.gson.Gson; import com.google.gson.GsonBuilder; /** * Gson处理这种一对多的序列化 * @author Ickes * */ public class DeptTest { public static void main(String[] args) { User user1 = new User("xl","xl_223",12100,new Date()); User user2 = new User("x2","xl_323",12300,new Date()); User user3 = new User("x3","xl_423",12300,new Date()); Dept dept = new Dept("D001","技术预研部"); List<User> users = new ArrayList<User>(); users.add(user1); users.add(user2); users.add(user3); dept.setUsers(users); Gson gson = new GsonBuilder() .setPrettyPrinting() .setDateFormat("yyyy-MM-dd HH:mm:ss") .create(); String json=gson.toJson(dept); System.out.println("一对多的序列化:\n"+json); dept = gson.fromJson(json,Dept.class); System.out.println("一对多的反序列化:\n"+dept); } }
返回结果如下:
一对多的序列化: { "id": "D001", "deptName": "技术预研部", "users": [ { "userName": "xl", "userPwd": "xl_223", "age": 12100, "birthday": "2015-01-13 18:57:23" }, { "userName": "x2", "userPwd": "xl_323", "age": 12300, "birthday": "2015-01-13 18:57:23" }, { "userName": "x3", "userPwd": "xl_423", "age": 12300, "birthday": "2015-01-13 18:57:23" } ] } 一对多的反序列化: Dept [id=D001, deptName=技术预研部, users=[User [userName=xl, userPwd=xl_223, age=12100, birthday=Tue Jan 13 18:57:23 CST 2015], User [userName=x2, userPwd=xl_323, age=12300, birthday=Tue Jan 13 18:57:23 CST 2015], User [userName=x3, userPwd=xl_423, age=12300, birthday=Tue Jan 13 18:57:23 CST 2015] ]]
四、带泛型的list集合处理
public static void main(String[] args) { Gson gson = new Gson(); String json="[{'last':'xiao','next':'ming'},{'last':'xiao','next':'liang'}]"; List<Dept> list= null; list = gson.fromJson(json,new TypeToken<List<Dept>>(){}.getType()); for (Dept dept : list) { System.out.println(dept); } }
返回结果如下:
Dept [last=xiao, next=ming] Dept [last=xiao, next=liang]
相关推荐
SXIAOYI 2020-09-16
xiaonuoyal 2020-06-17
SXIAOYI 2020-06-11
yinbaoshiguang 2020-06-09
Chydar 2020-06-02
nalanrumeng 2020-06-01
xiaonuoyal 2020-06-01
nalanrumeng 2020-05-19
ITprivate 2020-04-15
xiaonuoyal 2020-04-14
ITprivate 2020-03-26
ITprivate 2020-02-12
nalanrumeng 2020-01-13
xiaonuoyal 2019-12-07
xiaonuoyal 2019-11-18
TOmyhonour 2019-11-16