將Java的List構造經由過程GSON庫轉換為JSON的辦法示例。本站提示廣大學習愛好者:(將Java的List構造經由過程GSON庫轉換為JSON的辦法示例)文章只能為提供參考,不一定能成為您想要的結果。以下是將Java的List構造經由過程GSON庫轉換為JSON的辦法示例正文
發明了谷歌的gson,由於之前關於protocolbuf有一些懂得,帶著一些獵奇心,我開端應用了gson。
GitHub主頁:https://github.com/谷歌/gson
經由比擬,gson和其他現有java json類庫最年夜的分歧時gson須要序列化得實體類不須要應用annotation來標識須要序列化得字段,同時gson又可以經由過程應用annotation來靈巧設置裝備擺設須要序列化的字段。
將List或許Map轉換成json異常簡略:
public String getJsonData(List<?> list) { Gson gson = new Gson(); String jsonstring = gson.toJson(list); return jsonstring; }
上面我們會有具體的例子。
示例
簡略對象轉化和帶泛型的List轉化:
實體類:
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.谷歌.gson.Gson; import com.谷歌.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=柳波]