package com.json; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import com.google.gson.FieldNamingPolicy; import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.reflect.TypeToken; /** * 本文参考: http://blog.csdn.net/lk_blog/article/details/7685190 * */ public class JsonDemo { private List<Person> personList; private Person personDemo; private Gson gson; public void initPersonData() { personDemo = new Person("我是demo", 40, "中国", new Date()); Person p1 = new Person("张三", 20, "北京", new Date()); Person p2 = new Person("李四", 22, "上海", new Date()); Person p3 = new Person("王五", 30, "广州", new Date()); personList = new ArrayList<Person>(); personList.add(personDemo); personList.add(p1); personList.add(p2); personList.add(p3); } public static void main(String[] args) { JsonDemo demo = new JsonDemo(); int testType = 5; switch (testType) { case 1: // 简单对象转化和带泛型的List转化 demo.initPersonData(); demo.gson = new Gson(); demo.printPersonResult(); break; case 2: // Gson注解和GsonBuilder demo.initPersonData(); // 注意这里的Gson的构建方式为GsonBuilder,区别于test1中的Gson gson = new Gson(); demo.gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation() // 不导出实体中没有用@Expose注解的属性 .enableComplexMapKeySerialization() // 支持Map的key为复杂对象的形式 .serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss:SSS")// 时间转化为特定格式 .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)// 会把字段首字母大写,注:对于实体上使用了@SerializedName注解的不会生效. .setPrettyPrinting() // 对json结果格式化. .setVersion(1.0) // 有的字段不是一开始就有的,会随着版本的升级添加进来,那么在进行序列化和返序列化的时候就会根据版本号来选择是否要序列化. // @Since(版本号)能完美地实现这个功能.还的字段可能,随着版本的升级而删除,那么 // @Until(版本号)也能实现这个功能,GsonBuilder.setVersion(double)方法需要调用. .create(); demo.printPersonResult(); break; case 3:// Gson之实例-Map处理 demo.testMap1(); break; case 4:// Map处理 demo.testMap2(); break; case 5:// Map处理 demo.testMap3(); break; } } public void testMap1() { Map<Point, String> map1 = new LinkedHashMap<Point, String>();// 使用LinkedHashMap将结果按先进先出顺序排列 map1.put(new Point(3, 6), "p1"); map1.put(new Point(4, 8), "p2"); gson = new GsonBuilder().enableComplexMapKeySerialization().create(); String str = gson.toJson(map1); System.out.println(str);// 结果:[[{"x":3,"y":6},"p1"],[{"x":4,"y":8},"p2"]] Map<Point, String> retMap = gson.fromJson(str, new TypeToken<Map<Point, String>>() { }.getType()); for (Point p : retMap.keySet()) { System.out.println("key:" + p + ",values:" + retMap.get(p)); } System.out.println(retMap); } public void testMap2() { Map<String, Point> map1 = new LinkedHashMap<String, Point>();// 使用LinkedHashMap将结果按先进先出顺序排列 map1.put("a", new Point(3, 6)); map1.put("b", new Point(4, 8)); gson = new GsonBuilder().enableComplexMapKeySerialization().create(); String str = gson.toJson(map1); System.out.println(str);// 结果:[[{"x":3,"y":6},"p1"],[{"x":4,"y":8},"p2"]] Map<String, Point> retMap = gson.fromJson(str, new TypeToken<Map<String, Point>>() { }.getType()); for (String p : retMap.keySet()) { System.out.println("key:" + p + ",values:" + retMap.get(p)); } System.out.println(retMap); } public void testMap3() { initPersonData(); Map<String, Object> map = new HashMap<String, Object>(); map.put("persons", personList); List<Point> pointList = new ArrayList<Point>(); pointList.add(new Point(3, 6)); pointList.add(new Point(4, 6)); pointList.add(new Point(4, 7)); map.put("points", pointList); gson = new Gson(); String str = gson.toJson(map); System.out.println(str);// 结果:{"persons":[{"name":"我是demo","age":40,"address":"中国","bir":"Aug // 1, 2015 1:59:39 // PM"},{"name":"张三","age":20,"address":"北京","bir":"Aug // 1, 2015 1:59:39 // PM"},{"name":"李四","age":22,"address":"上海","bir":"Aug // 1, 2015 1:59:39 // PM"},{"name":"王五","age":30,"address":"广州","bir":"Aug // 1, 2015 1:59:39 // PM"}],"points":[{"x":3,"y":6},{"x":4,"y":6},{"x":4,"y":7}]} Map<String, Object> retMap = gson.fromJson(str, new TypeToken<Map<String, List<Object>>>() { }.getType()); for(String key:retMap.keySet()){ if("persons".equals(key)){ List<Person> pList = (List<Person>)retMap.get(key); System.out.println(pList); }else if("points".equals(key)){ List<Point> pList = (List<Point>)retMap.get(key); System.out.println(pList); } } } private void printPersonResult() { System.out.println("============简单对象的转化=============="); String p1json = gson.toJson(personDemo); System.out.println(p1json); Person refPerson = gson.fromJson(p1json, Person.class); System.out.println(refPerson); System.out.println(""); System.out.println("============带泛型的List的转化=============="); String jsonStr = gson.toJson(personList); System.out.println("jsonStr:" + jsonStr); List<Person> reflist = gson.fromJson(jsonStr, new TypeToken<List<Person>>() { }.getType()); for (Person per : reflist) { System.out.println(per); } } }
时间: 2024-10-08 00:15:48