Gson

本页使用了标题或全文手工转换
维基百科,自由的百科全书
Google Gson
开发者Google
首次发布2008年5月22日 (2008-05-22)
当前版本
  • 2.10.1 (2023年1月6日)[1]
编辑维基数据链接
原始码库 编辑维基数据链接
编程语言Java
操作系统跨平台
许可协议Apache License 2.0
网站github.com/google/gson

Gson(又称Google Gson)是Google公司发布的一个开放原始码的Java库,主要用途为序列化Java物件为JSON字符串,或反序列化JSON字符串成Java物件。

历史[编辑]

Gson当初是为因应Google公司内部需求而由Google自行研发而来,但自从在2008年五月公开发布第一版后已被许多公司或用户应用。

各版本发布时间[编辑]

  • 2008年
    • 05月22日 版本 1.0
    • 06月18日 版本 1.0.1
    • 07月1日 版本 1.1
    • 07月18日 版本 1.1.1
    • 08月29日 版本 1.2
  • 2009年
    • 01月12日 版本 1.3 Beta
    • 04月1日 版本 1.3
    • 10月9日 版本 1.4
  • 2010年
    • 08月19日 版本 1.5
    • 11月24日 版本 1.6
  • 2011年
    • 04月12日 版本 1.7
    • 04月13日 版本 1.7.1
    • 11月13日 版本 2.0
    • 12月31日 版本 2.1
  • 2012年
    • 05月5日 版本 2.2
    • 05月5日 版本 2.2.1
    • 07月2日 版本 2.2.2
  • 2013年
    • 04月12日 版本 2.2.3
    • 05月13日 版本 2.2.4
  • 2014年
    • 08月11日 版本 2.3
    • 11月20日 版本 2.3.1
  • 2015年
    • 10月4日 版本 2.4
    • 11月24日 版本 2.5
  • 2016年
    • 02月11日 版本 2.6
    • 02月11日 版本 2.6.1
    • 02月26日 版本 2.6.2
    • 06月14日 版本 2.7
    • 10月27日 版本 2.8.0
  • 2017年
    • 05月30日 版本 2.8.1

使用方法[编辑]

Gson的应用主要为toJson与fromJson两个转换函数,而在使用这种物件转换之前需先建立好物件的类别以及其成员才能成功的将JSON字符串成功转换成相对应的物件。

  class Examples {
    private int answer1 = 100;
    private String answer2 = "Hello world!";
    Examples(){
    }     // default constructor
  }

序列化JAVA物件成JSON字符串

   Examples example1 = new Examples();
   Gson gson = new Gson();
   String json = gson.toJson(example1);

==> json is {"answer1":100,"answer2":"Hello world!"}

反序列化JSON字符串成对应的JAVA物件

Examples example2= gson.fromJson(json,Examples.class);

==> example2即与example1相同

物件example1透过toJson序列化JSON字符串传递,再宣告一个物件example2为接收了JSON后透过fromJson反序列化成example2,故example1与example2相同

参考文献[编辑]

外部链接[编辑]

  1. ^ Release 2.10.1. 2023年1月6日 [2023年1月12日].