日常接口的数据传输通常使用xml或者json来传递数据,xml较庞大但是描述数据能力十分出众,json数据结构较小而且支持ajax传输,xml在数据传输和解析资源占用都比较逊色于json。因此日常的接口通常都使用json的数据格式进行传输。一方面能减少应用或者客户端如Android应用解析数据的资源占用,另一方面节省宝贵的数据传输时间。

Java中对Json的序列化和反序列化常用的工具有Json、Fastjson、Gson等。其中,Fastjson是阿里巴巴的开源产品,用Java语言实现,号称是速度最快的Json序列化工具。

今天学习了Gson工具,并做了Demo加深学习印象,直接贴上代码在代码中说明问题:

Person类:

 /*
* Copyright 2013 Alibaba.com All right reserved. This software is the
* confidential and proprietary information of Alibaba.com ("Confidential
* Information"). You shall not disclose such Confidential Information and shall
* use it only in accordance with the terms of the license agreement you entered
* into with Alibaba.com.
*/
package com.yunos.tv.common.util; import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken; /**
* 类GsonTest.java的实现描述:TODO 类实现描述
* @author riqi 2013年8月5日 下午10:26:28
*/ class Person { private String name; private int age; private Family family; public Person(String name, int age, Family family) {
super();
this.name = name;
this.age = age;
this.family = family;
} public Family getFamily() {
return family;
} public void setFamily(Family family) {
this.family = family;
} @Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", family=" + family + "]";
}
}

Family类:

 class Family {

     private String father;

     private String mother;

     public Family(String father, String mother) {
super();
this.father = father;
this.mother = mother;
} @Override
public String toString() {
return "Family [father=" + father + ", mother=" + mother + "]";
} }

测试过程:

 public class GsonTest {

     public static Gson gson = new Gson();

     public static void main(String[] args) {
String name = "riqi";
int age = 26;
Family family = new Family("爸爸", "妈妈"); // 序列化Map
Map<String, Object> userMap = new HashMap<String, Object>();
userMap.put("name", name);
userMap.put("age", age);
System.out.println(gson.toJson(userMap)); //{"age":26,"name":"riqi"} // 序列化List
List<Object> userList = new ArrayList<Object>();
userList.add(name);
userList.add(age);
System.out.println(gson.toJson(userList)); //["riqi",26] // 序列化对象
System.out.println(gson.toJson(family)); //{"father":"爸爸","mother":"妈妈"} // 借助TypeToken泛型实现单个对象序列化和反序列化
Person person = new Person(name, age, family);
Type personType = new TypeToken<Person>() {
}.getType();
String personJsonTo = gson.toJson(person, personType);
Person personJsonFrom = gson.fromJson(personJsonTo, personType);
Family familyJsonFrom = personJsonFrom.getFamily();
System.out.println(personJsonTo); //{"name":"riqi","age":26,"family":{"father":"爸爸","mother":"妈妈"}}
System.out.println(personJsonFrom);//Person [name=riqi, age=26, family=Family [father=爸爸, mother=妈妈]]
System.out.println(familyJsonFrom);//Family [father=爸爸, mother=妈妈] // 借助TypeToken泛型实现对象列表序列化和反序列化
List<Person> personList = new ArrayList<Person>();
Type personListType = new TypeToken<List<Person>>() {
}.getType();
for (int i = 0; i < 2; i++) {
Person personTmp = new Person(name, age + i, family); // 年龄做了变化
personList.add(personTmp);
}
String personListJsonTo = gson.toJson(personList, personListType);
List<Person> personListJsonFrom = gson.fromJson(personListJsonTo, personListType);
System.out.println(personListJsonTo);//[{"name":"riqi","age":26,"family":{"father":"爸爸","mother":"妈妈"}},{"name":"riqi","age":27,"family":{"father":"爸爸","mother":"妈妈"}}]
System.out.println(personListJsonFrom);//[Person [name=riqi, age=26, family=Family [father=爸爸, mother=妈妈]], Person [name=riqi, age=27, family=Family [father=爸爸, mother=妈妈]]]
}
}

对于日常的接口Json数据的序列化和反序列化,以上应该够用了。

Json工具类库之Gson实战笔记的更多相关文章

  1. JSON工具类库: alibaba/fastjson 使用记录

    JSON工具类库: alibaba/fastjson 使用记录 一.了解JSON JSON标准规范中文文档: http://www.json.org/json-zh.html 最佳实践:http:// ...

  2. Json 工具介绍 fastjson gson jackson

    1. fastjson fastjson是一个Java语言编写的高性能功能完善的JSON库.它采用一种“假定有序快速匹配”的算法,把JSON Parse的性能提升到极致, 是目前Java语言中最快的J ...

  3. 强大的json工具:fastJson

    fastJson   FastJSON是一个很好的java开源json工具类库,相比其他同类的json类库,它的速度的确是fast,最快!但是文档做得不好,在应用前不得不亲测一些功能.   实际上其他 ...

  4. Android进阶笔记17:3种JSON解析工具(org.json、fastjson、gson)

    一. 目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),其中解析速度最快的是Gson. 3种json工具下 ...

  5. Android进阶笔记14:3种JSON解析工具(org.json、fastjson、gson)

    一. 目前解析json有三种工具:org.json(Java常用的解析),fastjson(阿里巴巴工程师开发的),Gson(Google官网出的),其中解析速度最快的是Gson. 3种json工具下 ...

  6. Android Gson解析json工具类封装

    package com.springSecurity.gson; import java.util.ArrayList; import java.util.List; import java.util ...

  7. JSON数据解析(GSON方式) (转)

    JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种理想的数据交换格式. 在上一篇博文<Andro ...

  8. Java 的 JSON 开源类库选择比较(zz)

    在看了作者的介绍,然后我又到mvnrepository上去看了各个库的的使用数之后,发现只能在jackson和gson之间做选择. 以下是原文 有效选择七个关于Java的JSON开源类库 April  ...

  9. Java中json工具对比分析

    Java中几个json工具分析 1, 环境 JDK1.6+IDE(IntelliJ IDEA)+windowsXP+GBK编码 2,分析对象 jackson1.8.2 http://jackson.c ...

随机推荐

  1. ipv6 dns list

    ip v6 DNS list: 将首选DNS服务器地址填上2001:778::37 备用DNS服务器地址填上2001:df8:0:7::1

  2. unsupported major.minor version 解决方法

        转载自http://hi.baidu.com/fatchong/blog/item/191da23b478bbfef15cecbae.html         一直以来都是用jdk1.5,这次 ...

  3. 【设计模式 7】从公司的目前框架和API Gateway,谈谈对外观模式的理解

    我,第一次用到外观模式,应该是3年多以前.那时候是做一个收费系统,在当时的U层和B层之间,加了一层Facade.当时,在一些复杂的业务逻辑处理时,感受到了加入外观层的好处,但对于一些简单的(我指的是, ...

  4. MaterialUp 官方client源代码

    Material Design MaterialUp 官方client源代码  https://github.com/jariz/MaterialUp

  5. 转 spring官方文档中文版

    转 http://blog.csdn.net/tangtong1/article/details/51326887另附码云地址 https://gitee.com/free/spring-framew ...

  6. Shift Register(Using Submodule)

    /*************************************************** /  Shift Register module by Submodule /  Progra ...

  7. layui表格渲染中模板的使用举例

    实例一: { field: 'status', align: 'center', title: '活动状态', templet: function (d) { if (d.status == &quo ...

  8. 服务器上nginx反向代理的配置

    Nginx不但是一款高性能的Web服务器,也是高性能的反向代理服务器.下面简单说说Nginx的反向代理功能. 反向代理是什么? 反向代理指以代理服务器来接受Internet上的连接请求,然后将请求转发 ...

  9. 设计模式中类的关系之关联关系(Association)

    关联关系是类与类之间最常用的一种关系,它是一种结构化关系,用于表示一类对象与另一类对象之间有联系.它体现的是两个类.或者类与接口之间语义级别的一种强依赖关系,比如我和我的朋友.这种关系比依赖更强.不存 ...

  10. 在linux中使用sqlplus的上下左右箭头的方法

    在linux下面使用sqlpus连接数据库处理,实在不是很方便,还好有一个工具rlwrap,通过这个能够调用历史命令和回调功能,能够极大的提高效率. 1. 下载 从http://utopia.know ...