转载请出自出处:http://eksliang.iteye.com/blog/2175532
一、概述
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]
相关推荐
非常详细的json使用方法。包括对象和json之间的相互转换,集合和json之间的相互转换,自己按照节点解析。gson功能真的很强大。
Gson基本使用,数据容错处理 适配boolean、Boolean、int、Integer、float、Float、double、Double、long、Long、String类型。 还包括服务器返回的restful规范数据。
android Gson转换List集合demo,生成Json字符串,转换为List,对象
Gson的基本使用,特点:a、快速、高效b、代码量少、简洁c、面向对象d、数据传递和解析方便
android Gson使用实例 注释详细
使用gson解析json的一次测试使用
Gson类库的依赖jar包,导入jar包可以使用相关类库对JSON数据进行转化和解析。
Gson是Google的一个开源项目,可以将Java对象转换成JSON,也可能将JSON转换成Java对象。 Gson里最重要的对象有2个Gson 和 GsonBuilder Gson有2个最基本的方法 1) toJson() – 转换java 对象到JSON 2) from...
maven项目中gson使用方法.pdfmaven项目中gson使用方法.pdf
使用Gson解析Json和Json数组,通俗易懂
这种处理方式不需要依赖别的jar包,而且使用方法非常简单: List<String> list = new ArrayList(); list.add("zhangsan"); list.add("lisi"); list.add("wangwu"); Gson gson = new Gson(); String json = ...
gson使用.docgson使用.docgson使用.doc
Gson解析xml最强大的最方便的工具
介绍:Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. 压缩包里有以下文件: ...
om.google.code.gson/gson/2.6.2 Gson maven依赖 java 开发包
一个例子快速掌握Gson的基本使用方法 an example showing how to use Gson to accelerate development 对应的博客地址:http://blog.csdn.net/leoleohan/article/details/50886707
当json数据比较复杂的时候,可以使用解析对象的方式解析数据。用gson方法会容易的很多。
Gson库的jar包,三个包:gson-2.8.5.jar, gson-2.8.5-javadoc.jar, gson-2.8.5-sources.jar。
java解析json所需的三个jar包。包含org.json、Gson、fastjson三个jar包
网络上有很多Json解析库,这里我使用Gson来解析Json,Gson有个特点,就是要将数据的键作为变量封装到一个个实体中,如果值为数组的,变量的类型还必须是集合,然后通过Gson.fromJson来传入数据和实体类,再通过实体...