Json工具类库之Gson实战笔记
日常接口的数据传输通常使用xml或者json来传递数据,xml较庞大但是描述数据能力十分出众,json数据结构较小而且支持ajax传输,xml在数据传输和解析资源占用都比较逊色于json。因此日常的接口通常都使用json的数据格式进行传输。一方面能减少应用或者客户端如Android应用解析数据的资源占用,另一方面节省宝贵的数据传输时间。
Java中对Json的序列化和反序列化常用的工具有Json、Fastjson、Gson等。其中,Fastjson是阿里巴巴的开源产品,用Java语言实现,号称是速度最快的Json序列化工具。
今天学习了Gson工具,并做了Demo加深学习印象,直接贴上代码在代码中说明问题:
Person类:
1 /* 2 * Copyright 2013 Alibaba.com All right reserved. This software is the 3 * confidential and proprietary information of Alibaba.com ("Confidential 4 * Information"). You shall not disclose such Confidential Information and shall 5 * use it only in accordance with the terms of the license agreement you entered 6 * into with Alibaba.com. 7 */ 8 package com.yunos.tv.common.util; 9 10 import java.lang.reflect.Type; 11 import java.util.ArrayList; 12 import java.util.HashMap; 13 import java.util.List; 14 import java.util.Map; 15 16 import com.google.gson.Gson; 17 import com.google.gson.reflect.TypeToken; 18 19 /** 20 * 类GsonTest.java的实现描述:TODO 类实现描述 21 * @author riqi 2013年8月5日 下午10:26:28 22 */ 23 24 class Person { 25 26 private String name; 27 28 private int age; 29 30 private Family family; 31 32 public Person(String name, int age, Family family) { 33 super(); 34 this.name = name; 35 this.age = age; 36 this.family = family; 37 } 38 39 public Family getFamily() { 40 return family; 41 } 42 43 public void setFamily(Family family) { 44 this.family = family; 45 } 46 47 @Override 48 public String toString() { 49 return "Person [name=" + name + ", age=" + age + ", family=" + family + "]"; 50 } 51 }
Family类:
1 class Family { 2 3 private String father; 4 5 private String mother; 6 7 public Family(String father, String mother) { 8 super(); 9 this.father = father; 10 this.mother = mother; 11 } 12 13 @Override 14 public String toString() { 15 return "Family [father=" + father + ", mother=" + mother + "]"; 16 } 17 18 }
测试过程:
1 public class GsonTest { 2 3 public static Gson gson = new Gson(); 4 5 public static void main(String[] args) { 6 String name = "riqi"; 7 int age = 26; 8 Family family = new Family("爸爸", "妈妈"); 9 10 // 序列化Map 11 Map<String, Object> userMap = new HashMap<String, Object>(); 12 userMap.put("name", name); 13 userMap.put("age", age); 14 System.out.println(gson.toJson(userMap)); //{"age":26,"name":"riqi"} 15 16 // 序列化List 17 List<Object> userList = new ArrayList<Object>(); 18 userList.add(name); 19 userList.add(age); 20 System.out.println(gson.toJson(userList)); //["riqi",26] 21 22 // 序列化对象 23 System.out.println(gson.toJson(family)); //{"father":"爸爸","mother":"妈妈"} 24 25 // 借助TypeToken泛型实现单个对象序列化和反序列化 26 Person person = new Person(name, age, family); 27 Type personType = new TypeToken<Person>() { 28 }.getType(); 29 String personJsonTo = gson.toJson(person, personType); 30 Person personJsonFrom = gson.fromJson(personJsonTo, personType); 31 Family familyJsonFrom = personJsonFrom.getFamily(); 32 System.out.println(personJsonTo); //{"name":"riqi","age":26,"family":{"father":"爸爸","mother":"妈妈"}} 33 System.out.println(personJsonFrom);//Person [name=riqi, age=26, family=Family [father=爸爸, mother=妈妈]] 34 System.out.println(familyJsonFrom);//Family [father=爸爸, mother=妈妈] 35 36 // 借助TypeToken泛型实现对象列表序列化和反序列化 37 List<Person> personList = new ArrayList<Person>(); 38 Type personListType = new TypeToken<List<Person>>() { 39 }.getType(); 40 for (int i = 0; i < 2; i++) { 41 Person personTmp = new Person(name, age + i, family); // 年龄做了变化 42 personList.add(personTmp); 43 } 44 String personListJsonTo = gson.toJson(personList, personListType); 45 List<Person> personListJsonFrom = gson.fromJson(personListJsonTo, personListType); 46 System.out.println(personListJsonTo);//[{"name":"riqi","age":26,"family":{"father":"爸爸","mother":"妈妈"}},{"name":"riqi","age":27,"family":{"father":"爸爸","mother":"妈妈"}}] 47 System.out.println(personListJsonFrom);//[Person [name=riqi, age=26, family=Family [father=爸爸, mother=妈妈]], Person [name=riqi, age=27, family=Family [father=爸爸, mother=妈妈]]] 48 } 49 }
对于日常的接口Json数据的序列化和反序列化,以上应该够用了。