Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。可以将一个 JSON 字符串转成一个 Java 对象,或者反过来。

jar和源码下载地址: http://code.google.com/p/google-gson/downloads/list

实体类:

  1. public class Student {
  2. private int id;
  3. private String name;
  4. private Date birthDay;
  5. public int getId() {
  6. return id;
  7. }
  8. public void setId(int id) {
  9. this.id = id;
  10. }
  11. public String getName() {
  12. return name;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public Date getBirthDay() {
  18. return birthDay;
  19. }
  20. public void setBirthDay(Date birthDay) {
  21. this.birthDay = birthDay;
  22. }
  23. @Override
  24. public String toString() {
  25. return "Student [birthDay=" + birthDay + ", id=" + id + ", name="
  26. + name + "]";
  27. }
  28. }

测试类:

  1. import java.util.ArrayList;
  2. import java.util.Date;
  3. import java.util.List;
  4. import com.google.gson.Gson;
  5. import com.google.gson.reflect.TypeToken;
  6. public class GsonTest1 {
  7. public static void main(String[] args) {
  8. Gson gson = new Gson();
  9. Student student1 = new Student();
  10. student1.setId(1);
  11. student1.setName("李坤");
  12. student1.setBirthDay(new Date());
  13. // //////////////////////////////////////////////////////////
  14. System.out.println("----------简单对象之间的转化-------------");
  15. // 简单的bean转为json
  16. String s1 = gson.toJson(student1);
  17. System.out.println("简单Bean转化为Json===" + s1);
  18. // json转为简单Bean
  19. Student student = gson.fromJson(s1, Student.class);
  20. System.out.println("Json转为简单Bean===" + student);
  21. // 结果:
  22. // 简单Bean转化为Json==={"id":1,"name":"李坤","birthDay":"Jun 22, 2012 8:27:52 AM"}
  23. // Json转为简单Bean===Student [birthDay=Fri Jun 22 08:27:52 CST 2012, id=1,
  24. // name=李坤]
  25. // //////////////////////////////////////////////////////////
  26. Student student2 = new Student();
  27. student2.setId(2);
  28. student2.setName("曹贵生");
  29. student2.setBirthDay(new Date());
  30. Student student3 = new Student();
  31. student3.setId(3);
  32. student3.setName("柳波");
  33. student3.setBirthDay(new Date());
  34. List<Student> list = new ArrayList<Student>();
  35. list.add(student1);
  36. list.add(student2);
  37. list.add(student3);
  38. System.out.println("----------带泛型的List之间的转化-------------");
  39. // 带泛型的list转化为json
  40. String s2 = gson.toJson(list);
  41. System.out.println("带泛型的list转化为json==" + s2);
  42. // json转为带泛型的list
  43. List<Student> retList = gson.fromJson(s2,
  44. new TypeToken<List<Student>>() {
  45. }.getType());
  46. for (Student stu : retList) {
  47. System.out.println(stu);
  48. }
  49. // 结果:
  50. // 带泛型的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"}]
  51. // Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=1, name=李坤]
  52. // Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=2, name=曹贵生]
  53. // Student [birthDay=Fri Jun 22 08:28:52 CST 2012, id=3, name=柳波]
  54. }
  55. }

执行结果:

  1. ----------简单对象之间的转化-------------
  2. 简单Bean转化为Json==={"id":1,"name":"李坤","birthDay":"Jun 22, 2012 9:10:31 PM"}
  3. Json转为简单Bean===Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=1, name=李坤]
  4. ----------带泛型的List之间的转化-------------
  5. 带泛型的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"}]
  6. Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=1, name=李坤]
  7. Student [birthDay=Fri Jun 22 21:10:31 CST 2012, id=2, name=曹贵生]
  8. 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)

实例代码下载: http://download.csdn.net/detail/lk_blog/4387822

转自:链接

Json转换利器Gson之实例一-简单对象转化和带泛型的List转化 (转)的更多相关文章

  1. 从零开始学android开发-Json转换利器Gson之实例

    Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库.可以将一个 JSON 字符串转成一个 Java 对象,或者反过来. jar和源码下载地址: h ...

  2. java util - json转换工具 gson

    需要 gson-2.7.jar 包 package cn.java.gson; import com.google.gson.JsonElement; import com.google.gson.J ...

  3. json转换对象中出现null属性的解决方法

    前言:当数据进行json转换时,当属性值为null时,json解析就会中断,导致接下来的数据无法正确获取.原则上来讲服务器端发送的json字符串不允许存在属性值为空的情况,但是如果服务器端发送了nul ...

  4. Json转换工具类(基于google的Gson和阿里的fastjson)

    在项目之中我们经常会涉及到字符串和各种对象的转换,为此特地整理了一下常用的转换方法 一.基于com.google.code.gson封装的json转换工具类 1. 在pom.xml文件里面引入gson ...

  5. JSON库之性能比较:JSON.simple VS GSON VS Jackson VS JSONP

    从http://www.open-open.com/lib/view/open1434377191317.html 转载 Java中哪个JSON库的解析速度是最快的? JSON已经成为当前服务器与WE ...

  6. Json学习总结(2)——Java 下的 JSON库性能比较:JSON.simple vs. GSON vs. Jackson vs. JSONP

    JSON已经成为当前服务器与WEB应用之间数据传输的公认标准,不过正如许多我们所习以为常的事情一样,你会觉得这是理所当然的便不再深入思考了.我们很少会去想用到的这些JSON库到底有什么不同,但事实上它 ...

  7. [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类

    [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类 本节导读: 关于JSON序列化,不能 ...

  8. 使用DataContractJsonSerializer类将类型实例序列化为JSON字符串和反序列化为实例对象 分类: JSON 前端 2014-11-10 10:20 97人阅读 评论(1) 收藏

    一.JSON简介 JSON(JavaScript Object Notation,JavaScript对象表示法)是一种轻量级的数据交换格式. JSON是"名值对"的集合.结构由大 ...

  9. Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例

    Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例 继上篇json解析,我用了原生的json解析,但是在有些情况下我们不得不承认,一些优秀的json解析框架确实十分的 ...

随机推荐

  1. 对称加密之AES、压缩解压以及压缩加密解密解压综合实战

    AES 压缩解压 压缩加密解密解压 对称加密: 就是采用这种加密方法的双方使用方式用同样的密钥进行加密和解密.密钥是控制加密及解密过程的指令.算法是一组规则,规定如何进行加密和解密.   因此加密的安 ...

  2. hosts 本机DNS域名解析

    一. Hosts文件的位置很多用户都知道在Window系统中有个Hosts文件(没有后缀名),在Windows 98系统下该文件在Windows文件夹.在Windows 2000/XP系统中位于\%S ...

  3. Peeking Iterator

    Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...

  4. Match:Keywords Search(AC自动机模板)(HDU 2222)

    多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...

  5. JSP公用COMMON文件

    head.jsp: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" ...

  6. 【leetcode】Edit Distance (hard)

    Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...

  7. 【linux】ubuntu stmp服务器配置

    来源:http://blog.itpub.net/786540/viewspace-1002077/ sudo apt-get install sendmail(其中已经包含了sendmail-bin ...

  8. 无法定位序数4369于动态链接库libeay32.dll

    c:\windows\system32目录下应该有libeay32.dll,可能它过于陈旧,需要换一个新版本的libeay32.dll

  9. ASP.net绑定文本框Enter事件到按钮 ASP.NET执行后台执行JS方法

    txtAccountBarcode.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if (( ...

  10. Scanner和BufferedReader

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...