Gson 是 Google 提供的用來在 Java 對象和 JSON 數據之間進行映射的 Java 類庫。能夠將一個 JSON 字符串轉成一個 Java 對象,或者反過來。java
jar和源碼下載地址: http://code.google.com/p/google-gson/downloads/listjson
public class Student { private int id; private String name; private Date birthDay; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthDay() { return birthDay; } public void setBirthDay(Date birthDay) { this.birthDay = birthDay; } @Override public String toString() { return "Student [birthDay=" + birthDay + ", id=" + id + ", name=" + name + "]"; } }
測試類:ide
import java.util.ArrayList; import java.util.Date; import java.util.List; import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; public class GsonTest1 { public static void main(String[] args) { Gson gson = new Gson(); Student student1 = new Student(); student1.setId(1); student1.setName("李坤"); student1.setBirthDay(new Date()); // ////////////////////////////////////////////////////////// System.out.println("----------簡單對象之間的轉化-------------"); // 簡單的bean轉爲json String s1 = gson.toJson(student1); System.out.println("簡單Bean轉化爲Json===" + s1); // json轉爲簡單Bean Student student = gson.fromJson(s1, Student.class); System.out.println("Json轉爲簡單Bean===" + student); // 結果: // 簡單Bean轉化爲Json==={"id":1,"name":"李坤","birthDay":"Jun 22, 2012 8:27:52 AM"} // Json轉爲簡單Bean===Student [birthDay=Fri Jun 22 08:27:52 CST 2012, id=1, // name=李坤] // ////////////////////////////////////////////////////////// Student student2 = new Student(); student2.setId(2); student2.setName("曹貴生"); student2.setBirthDay(new Date()); Student student3 = new Student(); student3.setId(3); student3.setName("柳波"); student3.setBirthDay(new Date()); List<Student> list = new ArrayList<Student>(); list.add(student1); list.add(student2); list.add(student3); System.out.println("----------帶泛型的List之間的轉化-------------"); // 帶泛型的list轉化爲json String s2 = gson.toJson(list); System.out.println("帶泛型的list轉化爲json==" + s2); // json轉爲帶泛型的list List<Student> retList = gson.fromJson(s2, new TypeToken<List<Student>>() { }.getType()); for (Student stu : retList) { System.out.println(stu); } // 結果: // 帶泛型的list轉化爲json==[{"id":1,"name":"李坤","birthDay":"Jun 22, 2012 8:28:52 AM"},{"id":2,"name":"曹貴生","birthDay":"Jun 22, 2012 8:28:52 AM"},{"id":3,"name":"柳波","birthDay":"Jun 22, 2012 8:28:52 AM"}] // Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=1, name=李坤] // Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=2, name=曹貴生] // Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=3, name=柳波] } }
執行結果:測試
----------簡單對象之間的轉化------------- 簡單Bean轉化爲Json==={"id":1,"name":"李坤","birthDay":"Jun 22, 2012 9:10:31 PM"} Json轉爲簡單Bean===Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=1, name=李坤] ----------帶泛型的List之間的轉化------------- 帶泛型的list轉化爲json==[{"id":1,"name":"李坤","birthDay":"Jun 22, 2012 9:10:31 PM"},{"id":2,"name":"曹貴生","birthDay":"Jun 22, 2012 9:10:31 PM"},{"id":3,"name":"柳波","birthDay":"Jun 22, 2012 9:10:31 PM"}] Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=1, name=李坤] Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=2, name=曹貴生] Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=3, name=柳波]
Json轉換利器Gson之實例一-簡單對象轉化和帶泛型的List轉化 (http://blog.csdn.net/lk_blog/article/details/7685169)
Json轉換利器Gson之實例二-Gson註解和GsonBuilder (http://blog.csdn.net/lk_blog/article/details/7685190)
Json轉換利器Gson之實例三-Map處理(上) (http://blog.csdn.net/lk_blog/article/details/7685210)
Json轉換利器Gson之實例四-Map處理(下) (http://blog.csdn.net/lk_blog/article/details/7685224)
Json轉換利器Gson之實例五-實際開發中的特殊需求處理 (http://blog.csdn.net/lk_blog/article/details/7685237)
Json轉換利器Gson之實例六-註冊TypeAdapter及處理Enum類型 (http://blog.csdn.net/lk_blog/article/details/7685347)
ui
實例代碼下載: http://download.csdn.net/detail/lk_blog/4387822this