本文共 14185 字,大约阅读时间需要 47 分钟。
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()); ListuserList = new ArrayList (); userList.add(user1); userList.add(user2); userList.add(user3); Role role1 = new Role("1001", "chunlynn", "admin"); Role role2 = new Role("1002", "jeff", "vistor"); List roleList = new ArrayList (); roleList.add(role1); roleList.add(role2); Map > map = new LinkedHashMap >(); //可以 // Map map2 = new LinkedHashMap (); //可以 // Map
TypeToken
的构造方法是 protected
修饰的,所以上面才会写成new TypeToken<List<String>>(){}.getType() 而不是 new TypeToken<List<String>>().getType() 。 public 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]普通Map形式的泛型序列化与反序列化,Map的key为简单String形式 */ Map map1 = new LinkedHashMap (); 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 retMap1 = gson.fromJson(jsonStr1, new TypeToken