【Android開發經驗】超好用的json解析工具——Gson項目使用介紹

來源:互聯網
上載者:User

標籤:gson   json   google   

    轉載請註明出處:http://blog.csdn.net/zhaokaiqiang1992

    在上一篇文章中,我們簡單的介紹了json資料格式,並介紹了如何使用Android內建的json工具,完成json資料的產生與解析。但是就像我所說的,內建的工具類太難用了!因此這篇文章,會介紹一個json解析的非常好的工具,那就是google的開源項目Gson。

    咱們這次不介紹Gson裡面的常用類了,因為常用的幾個類非常的簡單,我們直接上手開始用,看看Gson是多麼的強大!

    當然,如果使用第三方的項目,我們肯定要匯入jar包了,一搜一大把哈。

    給出測試Model代碼

package com.example.jsondemo;public class Person {private String name;private int age;private Birthday birthday;public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public Birthday getBirthday() {return birthday;}public void setBirthday(Birthday birthday) {this.birthday = birthday;}public Person() {super();}public Person(String name, int age, Birthday birthday) {super();this.name = name;this.age = age;this.birthday = birthday;}@Overridepublic String toString() {return "Person [name=" + name + ", age=" + age + ", birthday="+ birthday + "]";}}


package com.example.jsondemo;/** *  * @ClassName: com.example.jsondemo.Birthday * @Description: 生日類 * @author zhaokaiqiang * @date 2014-11-26 上午10:29:47 *  */public class Birthday {private int year;private int month;private int day;public int getYear() {return year;}public void setYear(int year) {this.year = year;}public int getMonth() {return month;}public void setMonth(int month) {this.month = month;}public int getDay() {return day;}public void setDay(int day) {this.day = day;}public Birthday() {super();}public Birthday(int year, int month, int day) {super();this.year = year;this.month = month;this.day = day;}@Overridepublic String toString() {return "Birthday [year=" + year + ", month=" + month + ", day=" + day+ "]";}}



    1.Object與Json的相互轉換

    咱們先從最簡單的單一對象開始說。

    下面是代碼實現

/** * 單一對象的json產生與解析 */public void objectToJson() {Person p = new Person("zhaokaiqiang", 22, new Birthday(1992, 1, 19));Gson gson = new Gson();String jsonString = gson.toJson(p);Log.d(TAG, "---------單一對象產生--------");Log.d(TAG, jsonString);Person person = gson.fromJson(jsonString, Person.class);Log.d(TAG, "---------單一對象解析--------");Log.d(TAG, person.toString());}

    這代碼是多麼的簡潔,美觀啊!

    在上一篇的文章中,如果要產生簡單對象的話,我們還需要自己指定key,然後設定value,自從用了Gson,我們再也沒有這些煩惱了!而且為了說明Gson的強大之處,我還特意給Person設定了一個對象Birthday,如果使用內建的json工具,那不就麻煩死了。但是使用Gson,根本不需要有這些顧慮。看下面的轉換結果,一個toJson(),完全解決問題,太棒了!

11-26 16:16:38.392: D/GsonTest(16931): ---------單一對象產生--------11-26 16:16:38.392: D/GsonTest(16931): {"name":"zhaokaiqiang","birthday":{"day":19,"month":1,"year":1992},"age":22}11-26 16:16:38.392: D/GsonTest(16931): ---------單一對象解析--------11-26 16:16:38.392: D/GsonTest(16931): Person [name=zhaokaiqiang, age=22, birthday=Birthday [year=1992, month=1, day=19]]

    2.集合與Json的相互轉換

    Gson除了可以很方便的與單一對象進行轉換之外,還能夠與集合進行轉換。

    下面是一個產生Json,並完成解析的代碼:

/** * 集合對象的json產生與解析 */public void objectsToJson() {Gson gson = new Gson();Person person = new Person("zhaokaiqiang", 22,new Birthday(1992, 1, 19));ArrayList<Person> arrayList = new ArrayList<Person>();arrayList.add(person);arrayList.add(person);arrayList.add(person);String jsonString = gson.toJson(arrayList);Log.d(TAG, "---------集合對象產生--------");Log.d(TAG, jsonString);Type type = new TypeToken<ArrayList<Person>>() {}.getType();ArrayList<Person> persons = gson.fromJson(jsonString, type);Log.d(TAG, "---------集合對象解析--------");Log.d(TAG, persons.toString());}

    這是轉換出來的結果

---------集合對象產生--------[{"name":"zhaokaiqiang","birthday":{"day":19,"month":1,"year":1992},"age":22},{"name":"zhaokaiqiang","birthday":{"day":19,"month":1,"year":1992},"age":22},{"name":"zhaokaiqiang","birthday":{"day":19,"month":1,"year":1992},"age":22}] ---------集合對象解析-------- [Person [name=zhaokaiqiang, age=22, birthday=Birthday [year=1992, month=1, day=19]], Person [name=zhaokaiqiang, age=22, birthday=Birthday [year=1992, month=1, day=19]], Person [name=zhaokaiqiang, age=22, birthday=Birthday [year=1992, month=1, day=19]]]

    還記得我們使用json如果想完成類似的功能的時候,是不可想象的,但是使用Gson來實現的話,確實如此的簡單。

    在使用Gson.toJson()方法的時候,我們可以把任何對象或者是集合放進去,就可以完美的轉換成json格式。

    如果想把json格式的資料轉換成我們想要的對象,我們只需要調用Gson.fromJson()即可。

    但是在轉換的時候,第二個參數有兩種形式,一種是Class<?>,這個是轉換單一的對象的時候,我們需要把對象.class傳遞進去,告訴轉換器要轉換的類型。如果我們需要轉換的是集合,我們需要傳遞一個實現Type介面的執行個體,具體文法就和上面的代碼一樣,集合類型還有具體的對象換一下就可以了。


【Android開發經驗】超好用的json解析工具——Gson項目使用介紹

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.