Json转换利器Gson之实例一-简单对象转化和带泛型的List转化 (转)
Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。可以将一个 JSON 字符串转成一个 Java 对象,或者反过来。
jar和源码下载地址: http://code.google.com/p/google-gson/downloads/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.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)
实例代码下载: http://download.csdn.net/detail/lk_blog/4387822
转自:链接
Json转换利器Gson之实例一-简单对象转化和带泛型的List转化 (转)的更多相关文章
- 从零开始学android开发-Json转换利器Gson之实例
Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库.可以将一个 JSON 字符串转成一个 Java 对象,或者反过来. jar和源码下载地址: h ...
- java util - json转换工具 gson
需要 gson-2.7.jar 包 package cn.java.gson; import com.google.gson.JsonElement; import com.google.gson.J ...
- json转换对象中出现null属性的解决方法
前言:当数据进行json转换时,当属性值为null时,json解析就会中断,导致接下来的数据无法正确获取.原则上来讲服务器端发送的json字符串不允许存在属性值为空的情况,但是如果服务器端发送了nul ...
- Json转换工具类(基于google的Gson和阿里的fastjson)
在项目之中我们经常会涉及到字符串和各种对象的转换,为此特地整理了一下常用的转换方法 一.基于com.google.code.gson封装的json转换工具类 1. 在pom.xml文件里面引入gson ...
- JSON库之性能比较:JSON.simple VS GSON VS Jackson VS JSONP
从http://www.open-open.com/lib/view/open1434377191317.html 转载 Java中哪个JSON库的解析速度是最快的? JSON已经成为当前服务器与WE ...
- Json学习总结(2)——Java 下的 JSON库性能比较:JSON.simple vs. GSON vs. Jackson vs. JSONP
JSON已经成为当前服务器与WEB应用之间数据传输的公认标准,不过正如许多我们所习以为常的事情一样,你会觉得这是理所当然的便不再深入思考了.我们很少会去想用到的这些JSON库到底有什么不同,但事实上它 ...
- [.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类
[.net 面向对象程序设计进阶] (13) 序列化(Serialization)(五) Json 序列化利器 Newtonsoft.Json 及 通用Json类 本节导读: 关于JSON序列化,不能 ...
- 使用DataContractJsonSerializer类将类型实例序列化为JSON字符串和反序列化为实例对象 分类: JSON 前端 2014-11-10 10:20 97人阅读 评论(1) 收藏
一.JSON简介 JSON(JavaScript Object Notation,JavaScript对象表示法)是一种轻量级的数据交换格式. JSON是"名值对"的集合.结构由大 ...
- Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例
Android JSON解析库Gson和Fast-json的使用对比和图书列表小案例 继上篇json解析,我用了原生的json解析,但是在有些情况下我们不得不承认,一些优秀的json解析框架确实十分的 ...
随机推荐
- 对称加密之AES、压缩解压以及压缩加密解密解压综合实战
AES 压缩解压 压缩加密解密解压 对称加密: 就是采用这种加密方法的双方使用方式用同样的密钥进行加密和解密.密钥是控制加密及解密过程的指令.算法是一组规则,规定如何进行加密和解密. 因此加密的安 ...
- hosts 本机DNS域名解析
一. Hosts文件的位置很多用户都知道在Window系统中有个Hosts文件(没有后缀名),在Windows 98系统下该文件在Windows文件夹.在Windows 2000/XP系统中位于\%S ...
- Peeking Iterator
Given an Iterator class interface with methods: next() and hasNext(), design and implement a Peeking ...
- Match:Keywords Search(AC自动机模板)(HDU 2222)
多模匹配 题目大意:给定很多个字串A,B,C,D,E....,然后再给你目标串str字串,看目标串中出现多少个给定的字串. 经典AC自动机模板题,不多说. #include <iostream& ...
- JSP公用COMMON文件
head.jsp: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" ...
- 【leetcode】Edit Distance (hard)
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2 ...
- 【linux】ubuntu stmp服务器配置
来源:http://blog.itpub.net/786540/viewspace-1002077/ sudo apt-get install sendmail(其中已经包含了sendmail-bin ...
- 无法定位序数4369于动态链接库libeay32.dll
c:\windows\system32目录下应该有libeay32.dll,可能它过于陈旧,需要换一个新版本的libeay32.dll
- ASP.net绑定文本框Enter事件到按钮 ASP.NET执行后台执行JS方法
txtAccountBarcode.Attributes.Add("onkeydown", "if(event.which || event.keyCode){if (( ...
- Scanner和BufferedReader
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...