JSON解析类库之Gson(2) --- 泛型对象Map、List与JSON字符串互转
---Gson类库学习, 生成与解析json数据,json字符串与Java对象互转
Gson gson = new GsonBuilder()//.setPrettyPrinting()//格式化输出(序列化).setDateFormat("yyyy-MM-dd HH:mm:ss") //序列化日期格式化输出.create();User user1 = new User("1", "王重阳", new Date());User user2 = new User("2", "郭靖", new Date());User user3 = new User("3", "黄蓉", new Date());User[] usersArray = { user1, user2, user3 }; //数组对象/*** 序列化数组*/String jsonArrString = gson.toJson(usersArray);System.out.println("数组序列化 ==》 " + jsonArrString);/*** 数组序列化 ==》 [{"id": "1","name": "王重阳","birthday": "2017-05-03 16:06:50"},{"id": "2","name": "郭靖","birthday": "2017-05-03 16:06:50"},{"id": "3","name": "黄蓉","birthday": "2017-05-03 16:06:50"}]*//*** JSON字符串反序列化成数组对象,数组反序列化的类型参是可以直接用数组的class, 如 User [].class*/User[] usersArr = gson.fromJson(jsonArrString, User[].class);for (int i = 0; i < usersArr.length; i++) {User u1 = usersArr[i];System.out.println("JSON字符串反序列化成数组对象 ==》 " + u1);/** User对象 ==》 User [id=1, name=王重阳, birthday=Wed May 03 16:14:14 CST 2017]User对象 ==》 User [id=2, name=郭靖, birthday=Wed May 03 16:14:14 CST 2017]User对象 ==》 User [id=3, name=黄蓉, birthday=Wed May 03 16:14:14 CST 2017]*/}
public class Role {private String id;private String name;private String title;public Role() {super();}public Role(String id, String name, String title) {super();this.id = id;this.name = name;this.title = title;}//为了代码简洁,这里移除了getter和setter方法、toString方法等}
package com.chunlynn.gson;import java.lang.reflect.Type;import java.util.ArrayList;import java.util.Date;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import com.google.gson.reflect.TypeToken;public class GsonTest10 {public static void main(String[] args) {Gson gson = new GsonBuilder()//.setPrettyPrinting()//格式化输出(序列化).setDateFormat("yyyy-MM-dd HH:mm:ss") //序列化日期格式化输出.create();User user1 = new User("1", "王重阳", new Date());User user2 = new User("2", "郭靖", new Date());User user3 = new User("3", "黄蓉", new Date());List<User> userList = new ArrayList<User>();userList.add(user1);userList.add(user2);userList.add(user3);Role role1 = new Role("1001", "chunlynn", "admin");Role role2 = new Role("1002", "jeff", "vistor");List<Role> roleList = new ArrayList<Role>();roleList.add(role1);roleList.add(role2);Map<String, List extends Object>> map = new LinkedHashMap<String, List extends Object>>(); //可以// Mapmap2 = new LinkedHashMap (); //可以 // Map> map3 = new LinkedHashMap >();//这种不行,类型不匹配,put时无法添加 map.put("users", userList);map.put("roles", roleList);/*** [7] Map泛型序列化成JSON字符串*/String jsonStr = gson.toJson(map);System.out.println("Map泛型序列化成JSON字符串 ==》 " + jsonStr);/*Map泛型序列化成JSON字符串 ==》{"users": [{"id": "1","name": "王重阳","birthday": "2017-05-03 17:04:12"},{"id": "2","name": "郭靖","birthday": "2017-05-03 17:04:12"},{"id": "3","name": "黄蓉","birthday": "2017-05-03 17:04:12"}],"roles": [{"id": "1001","name": "chunlynn","title": "admin"},{"id": "1002","name": "jeff","title": "vistor"}]}*//*** [8] JSON字符串反序列化成包含Map泛型类型*/// 反序列化成那种泛型类型的设置,Map的泛型类型Type mtype = new TypeToken<Map<String, List<Object>>>() {}.getType();Map<String, List<Object>> retMap = gson.fromJson(jsonStr, mtype);System.out.println("retMap == > " + retMap);for (Map.Entry<String, List<Object>> p : retMap.entrySet()) {System.out.println("key ==" + p.getKey() + " , " + "value==" + p.getValue());/** key ==users , value==[{id=1, name=王重阳, birthday=2017-05-03 17:55:16}, {id=2, name=郭靖, birthday=2017-05-03 17:55:16}, {id=3, name=黄蓉, birthday=2017-05-03 17:55:16}]key ==roles , value==[{id=1001, name=chunlynn, title=admin}, {id=1002, name=jeff, title=vistor}]*/}Map<String, Object> retMap2 = gson.fromJson(jsonStr, mtype);System.out.println("retMap2 == > " + retMap2);for (Map.Entry<String, Object> p : retMap2.entrySet()) {System.out.println("key2 ==" + p.getKey() + " , " + "value2==" + p.getValue());/** key2 ==users , value2==[{id=1, name=王重阳, birthday=2017-05-03 17:55:16}, {id=2, name=郭靖, birthday=2017-05-03 17:55:16}, {id=3, name=黄蓉, birthday=2017-05-03 17:55:16}]key2 ==roles , value2==[{id=1001, name=chunlynn, title=admin}, {id=1002, name=jeff, title=vistor}]*/}Map<String, List extends Object>> retMap3 = gson.fromJson(jsonStr, mtype);System.out.println("retMap3 == > " + retMap3);for (Map.Entry<String, List extends Object>> p : retMap3.entrySet()) {System.out.println("key3 ==" + p.getKey() + " , " + "value3==" + p.getValue());/** key3 ==users , value3==[{id=1, name=王重阳, birthday=2017-05-03 17:55:16}, {id=2, name=郭靖, birthday=2017-05-03 17:55:16}, {id=3, name=黄蓉, birthday=2017-05-03 17:55:16}]key3 ==roles , value3==[{id=1001, name=chunlynn, title=admin}, {id=1002, name=jeff, title=vistor}]*/}/*retMap == > {users=[{id=1, name=王重阳, birthday=2017-05-03 17:43:05}, {id=2, name=郭靖, birthday=2017-05-03 17:43:05}, {id=3, name=黄蓉, birthday=2017-05-03 17:43:05}],roles=[{id=1001, name=chunlynn, title=admin}, {id=1002, name=jeff, title=vistor}]}retMap2 == > {users=[{id=1, name=王重阳, birthday=2017-05-03 17:43:05}, {id=2, name=郭靖, birthday=2017-05-03 17:43:05}, {id=3, name=黄蓉, birthday=2017-05-03 17:43:05}],roles=[{id=1001, name=chunlynn, title=admin}, {id=1002, name=jeff, title=vistor}]}retMap3 == > {users=[{id=1, name=王重阳, birthday=2017-05-03 17:43:05}, {id=2, name=郭靖, birthday=2017-05-03 17:43:05}, {id=3, name=黄蓉, birthday=2017-05-03 17:43:05}],roles=[{id=1001, name=chunlynn, title=admin}, {id=1002, name=jeff, title=vistor}]}*/}}
TypeToken
的构造方法是
protected
修饰的,所以上面才会写成new TypeTokenpublic class Point {private int x;private int y;public Point(int x, int y) {super();this.x = x;this.y = y;}//为了代码简洁,这里移除了getter和setter方法、toString方法等}
package com.chunlynn.gson;import java.util.LinkedHashMap;import java.util.Map;import com.google.gson.Gson;import com.google.gson.GsonBuilder;import com.google.gson.reflect.TypeToken;public class GsonTest11 {public static void main(String[] args) {Gson gson = new GsonBuilder()////.setPrettyPrinting()//格式化输出(序列化).enableComplexMapKeySerialization() ////支持Map的key为复杂对象的形式.create();/*** [9]普通MapObject> 形式的泛型序列化与反序列化,Map的key为简单String形式*/Map<String, Point> map1 = new LinkedHashMap<String, Point>();map1.put("a", new Point(3, 4));map1.put("b", new Point(5, 6));String jsonStr1 = gson.toJson(map1);System.out.println("普通Map对象的序列化 ===》 " + jsonStr1);// 普通Map对象的序列化 ===》 {"a":{"x":3,"y":4},"b":{"x":5,"y":6}}Map<String, Point> retMap1 = gson.fromJson(jsonStr1, new TypeToken<Map<String, Point>>() {}.getType());for (String key : retMap1.keySet()) {System.out.println("key=" + key + ", values=" + retMap1.get(key));}//key=a, values=Point [x=3, y=4]//key=b, values=Point [x=5, y=6]/*** [10]特殊Map<Object,String>形式的泛型序列化与反序列化,Map的key为复杂对象形式*/Map<Point, String> map2 = new LinkedHashMap<Point, String>();map2.put(new Point(7, 8), "c");map2.put(new Point(9, 10), "d");String jsonStr2 = gson.toJson(map2);System.out.println("Map的key为复杂对象形式的Map对象的序列化 ===》 " + jsonStr2);//Map的key为复杂对象形式的Map对象的序列化 ===》 [[{"x":7,"y":8},"c"],[{"x":9,"y":10},"d"]]Map<Point, String> retMap2 = gson.fromJson(jsonStr2, new TypeToken<Map<Point, String>>() {}.getType());for (Point pKey : retMap2.keySet()) {System.out.println("key=" + pKey + ", values:" + retMap2.get(pKey));}// key=Point [x=7, y=8], values:c// key=Point [x=9, y=10], values:d}}