Gson

维基百科,自由的百科全书
跳转到导航 跳转到搜索
Google Gson
开发者Google
首次发布2008年5月22日 (2008-05-22)
当前版本2.8.1(2017年5月30日,​9年前​(2017-05-30
源代码库
  • {{URL|example.com|可选的显示文本}}
Module:EditAtWikidata第29行Lua错误:attempt to index field 'wikibase' (a nil value)
编程语言Java
引擎
    Module:EditAtWikidata第29行Lua错误:attempt to index field 'wikibase' (a nil value)
    操作系统跨平台
    许可协议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相同

    参考文献[编辑]

    外部链接[编辑]