將Java的List結(jié)構(gòu)通過GSON庫轉(zhuǎn)換為JSON的方法示例
發(fā)現(xiàn)了google的gson,因為之前對于protocolbuf有一些了解,帶著一些好奇心,我開始使用了gson。
GitHub主頁:https://github.com/google/gson
經(jīng)過比較,gson和其他現(xiàn)有java json類庫最大的不同時gson需要序列化得實體類不需要使用annotation來標識需要序列化得字段,同時gson又可以通過使用annotation來靈活配置需要序列化的字段。
將List或者Map轉(zhuǎn)換成json非常簡單:
public String getJsonData(List<?> list) { Gson gson = new Gson(); String jsonstring = gson.toJson(list); return jsonstring; }
下面我們會有詳細的例子。
示例
簡單對象轉(zhuǎn)化和帶泛型的List轉(zhuǎn)化:
實體類:
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 + "]"; } }
測試類:
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("----------簡單對象之間的轉(zhuǎn)化-------------"); // 簡單的bean轉(zhuǎn)為json String s1 = gson.toJson(student1); System.out.println("簡單Bean轉(zhuǎn)化為Json===" + s1); // json轉(zhuǎn)為簡單Bean Student student = gson.fromJson(s1, Student.class); System.out.println("Json轉(zhuǎn)為簡單Bean===" + student); // 結(jié)果: // 簡單Bean轉(zhuǎn)化為Json==={"id":1,"name":"李坤","birthDay":"Jun 22, 2012 8:27:52 AM"} // Json轉(zhuǎn)為簡單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之間的轉(zhuǎn)化-------------"); // 帶泛型的list轉(zhuǎn)化為json String s2 = gson.toJson(list); System.out.println("帶泛型的list轉(zhuǎn)化為json==" + s2); // json轉(zhuǎn)為帶泛型的list List<Student> retList = gson.fromJson(s2, new TypeToken<List<Student>>() { }.getType()); for (Student stu : retList) { System.out.println(stu); } // 結(jié)果: // 帶泛型的list轉(zhuǎn)化為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=柳波] } }
執(zhí)行結(jié)果:
----------簡單對象之間的轉(zhuǎn)化------------- 簡單Bean轉(zhuǎn)化為Json==={"id":1,"name":"李坤","birthDay":"Jun 22, 2012 9:10:31 PM"} Json轉(zhuǎn)為簡單Bean===Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=1, name=李坤] ----------帶泛型的List之間的轉(zhuǎn)化------------- 帶泛型的list轉(zhuǎn)化為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=柳波]
相關(guān)文章
Java之SpringBoot集成ActiveMQ消息中間件案例講解
這篇文章主要介紹了Java之SpringBoot集成ActiveMQ消息中間件案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07spring?aop?pointcut?添加多個execution方式
這篇文章主要介紹了spring?aop?pointcut?添加多個execution方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11Springboot使用JustAuth實現(xiàn)各種第三方登陸
本文主要介紹了Springboot使用JustAuth實現(xiàn)各種第三方登陸,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07Java中ResultSetMetaData 元數(shù)據(jù)的具體使用
本文主要介紹了Java中ResultSetMetaData 元數(shù)據(jù)的具體使用,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04