标签:
google的Gson封装的比较彻底,实现简单:
一、下载Gson的jar包:gson-2.2.4.jar
二、新建工程,导入jar包。
三、新建实体类:
package com.cn.entity; public class User { private String name; private Integer age; private String address; public User(){ } public User(String name, Integer age, String address) { super(); this.name = name; this.age = age; this.address = address; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
四、新建静态工具类实现Gson对象只实例化一次:
JsonToObject.java--->>>
package com.cn.util; import com.google.gson.Gson; public class JsonToObject { private static final Gson googleJson = new Gson(); /** * 获取Gson常量,减少重复创建,在编译后就完成改变量初始化 * @return */ public static Gson getGoogleJson(){ return googleJson; } /** * 直接获取 * @param <T> * @param json * @param class1 * @return */ public static <T> Object jsonToObject(String json , Class<T> class1){ return googleJson.fromJson(json, class1); } }
ObjectToJsonUtil.java--->>>
package com.cn.util; import com.google.gson.Gson; public class ObjectToJsonUtil { private static final Gson googleJson = new Gson(); /** * 获取Gson常量,减少重复创建,在编译后就完成改变量初始化 * @return */ public static Gson getGoogleJson(){ return googleJson; } public static String objectToJson(Object object){ return googleJson.toJson(object); } }
五、建立测试类,进行测试:
package com.cn.test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import com.cn.entity.User; import com.cn.util.JsonToObject; import com.cn.util.ObjectToJsonUtil; import com.google.gson.reflect.TypeToken; public class Test { public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException { //对象转json字符串 User zhang = new User("zhang", 21, "深圳"); System.out.println("张某某转为json字符串:"+ObjectToJsonUtil.objectToJson(zhang)); //json转对象 String json = "{‘name‘:‘wang‘,‘age‘:23,‘address‘:‘北京‘}"; User wang = (User)JsonToObject.jsonToObject(json, User.class); System.out.println("json字符串转为王某某:"+"名字-"+wang.getName()+",年龄-"+wang.getAge()+",地址-"+wang.getAddress()); //字符串数组转json String[] strs = {"中国","湖北","武汉"}; System.out.println(ObjectToJsonUtil.objectToJson(strs)); //list对象转json List<User> list = new ArrayList<User>(); list.add(new User("张三", 21, "北京")); list.add(new User("李四", 22, "上海")); list.add(new User("王五", 23, "深圳")); System.out.println(ObjectToJsonUtil.objectToJson(list)); //map对象转json Map<String, User> map = new HashMap<String, User>(); map.put("第一名", new User("张三", 21, "北京")); map.put("第二名", new User("李四", 22, "上海")); map.put("第三名", new User("王五", 23, "深圳")); System.out.println(ObjectToJsonUtil.objectToJson(map)); //json转list集合 String jsonList = "[{‘name‘:‘zhang‘,‘age‘:21,‘address‘:‘北京‘},{‘name‘:‘wang‘,‘age‘:22,‘address‘:‘上海‘},{‘name‘:‘zhao‘,‘age‘:23,‘address‘:‘深圳‘}]"; List<User> personList = JsonToObject.getGoogleJson().fromJson(jsonList, new TypeToken<List<User>>(){}.getType()); System.out.println(personList.get(0).getName()+"---"+personList.get(1).getName()+"---"+personList.get(2).getName()); //json转map集合 String jsonMap = "{‘one‘:{‘name‘:‘zhang‘,‘age‘:21,‘address‘:‘北京‘},‘two‘:{‘name‘:‘wang‘,‘age‘:22,‘address‘:‘上海‘},‘three‘:{‘name‘:‘zhao‘,‘age‘:23,‘address‘:‘深圳‘}}"; Map<String, User> personMap = JsonToObject.getGoogleJson().fromJson(jsonMap, new TypeToken<Map<String , User>>(){}.getType()); System.out.println("第一名:"+personMap.get("one").getName()+" 第二名:"+personMap.get("two").getName()+" 第三名:"+personMap.get("three").getName()); } }
总结:主要使用Gson对象的toJson实现对象序列化成json字符串 和fromJson实现把json字符串反序列化成对象。在把json转成List和Map集合的时候
使用TypeToken代理对象实现转换。
标签:
原文地址:http://www.cnblogs.com/songzhen/p/5765802.html