詳解Gson使用(一)簡單對象轉化

感謝原文主,內容很詳細,很不錯

原文鏈接


JSON是一種輕量級的數據交換格式。 易於人閱讀和編寫,同時也易於機器解析和生成(一般用於提升網絡傳輸速率)

在之前我寫過一篇文章,《Android解析json數據

http://blog.csdn.net/a249900679/article/details/51195505

介紹了json和用JSONObjectJSONArray解析json數據的方法,接下來幾篇文章將會介紹解析json數據的更好的方法:使用Gson解析。

 

GsonGoogle推出的用來解析json數據以及將對象轉換成json數據的框架。可以很方便地實現json數據與對象的相互轉換,還可以自定義需要序列化或反序列化的字段。

 

使用Gson需要先導入jar,我這裏用到的是gson-2.3.1.jar

項目和jar下載地址:

Githubhttps://github.com/smileysx/GsonTest

Oschinahttps://git.oschina.net/ysx_xx/GsonText



詳解Gson使用(一)簡單對象轉化

http://blog.csdn.net/a249900679/article/details/51385913

詳解Gson使用(二)帶泛型的List轉化

http://blog.csdn.net/a249900679/article/details/51386028

詳解Gson使用(三)使用註解

http://blog.csdn.net/a249900679/article/details/51386509

詳解Gson使用(四)Map對象轉化
http://blog.csdn.net/a249900679/article/details/51386660
詳解Gson使用(五)實現百度翻譯功能

http://blog.csdn.net/a249900679/article/details/51386727


其中gson-2.3.1.jar在項目lib目錄下

 

下面先來介紹簡單對象的轉換:

注意:以下所有實體類的變量名要跟json數據中的key相同

1.普通json數據對象實體類:

  1. public class ToJsonBeanOne {  
  2.     private int id;  
  3.     private String name;  
  4.     private int age;  
  5.   
  6.     public ToJsonBeanOne(int id, String name, int age) {  
  7.         super();  
  8.         this.id = id;  
  9.         this.name = name;  
  10.         this.age = age;  
  11.     }  
  12.   
  13.     public int getId() {  
  14.         return id;  
  15.     }  
  16.   
  17.     public String getName() {  
  18.         return name;  
  19.     }  
  20.   
  21.     public int getAge() {  
  22.         return age;  
  23.     }  
  24.   
  25.     public void setId(int id) {  
  26.         this.id = id;  
  27.     }  
  28.   
  29.     public void setName(String name) {  
  30.         this.name = name;  
  31.     }  
  32.   
  33.     public void setAge(int age) {  
  34.         this.age = age;  
  35.     }  
  36.   
  37.     @Override  
  38.     public String toString() {  
  39.         String resultString = "";  
  40.         resultString += "id:" + id + "\nname:" + name + "\nage:" + age + "\n";  
  41.   
  42.         return resultString;  
  43.     }  
  44. }  

 

可以看出來,上面對象中三個數據都是String類型,這是最簡單的。

看看如何把該對象序列化:

  1. public class ToJsonTest extends Activity {  
  2.   
  3.     private TextView show;  
  4.     private Button start;  
  5.     private Gson gson;  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.   
  12.         initData();  
  13.     }  
  14.   
  15.     private void initData() {  
  16.   
  17.         gson = new Gson();  
  18.         show = (TextView) findViewById(R.id.showtext);  
  19.         start = (Button) findViewById(R.id.send);  
  20.         start.setOnClickListener(new OnClickListener() {  
  21.   
  22.             @Override  
  23.             public void onClick(View v) {  
  24.                 showData();  
  25.             }  
  26.         });  
  27.     }  
  28.   
  29.     private void showData() {  
  30.   
  31.         String resultString = "";  
  32.   
  33.         resultString = one() + "\n\n";  
  34.   
  35.         show.setText(resultString);  
  36.     }  
  37.   
  38.     private String one() {  
  39.         //創建對象  
  40.         ToJsonBeanOne toJsonBeanOne = new ToJsonBeanOne(1"小熊"21);  
  41.         //將對象轉換爲json數據  
  42.         return gson.toJson(toJsonBeanOne);  
  43.     }  
  44. }  

結果爲:



接下來看看如何把json數據轉換爲該對象:

  1. public class FromJsonTest extends Activity {  
  2.   
  3.     /** 
  4.      * 顯示數據的textview 
  5.      */  
  6.     private TextView show;  
  7.     /** 
  8.      * 按鈕 
  9.      */  
  10.     private Button start;  
  11.     /** 
  12.      * gson 
  13.      */  
  14.     private Gson gson;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.   
  21.         initData();  
  22.     }  
  23.   
  24.     private void initData() {  
  25.   
  26.         gson = new Gson();  
  27.         show = (TextView) findViewById(R.id.showtext);  
  28.         start = (Button) findViewById(R.id.send);  
  29.         start.setOnClickListener(new OnClickListener() {  
  30.   
  31.             @Override  
  32.             public void onClick(View v) {  
  33.                 showData();  
  34.             }  
  35.         });  
  36.   
  37.     }  
  38.   
  39.     private void showData() {  
  40.         String showString = "";  
  41.   
  42.         showString += one();  
  43.   
  44.         show.setText(showString);  
  45.   
  46.     }  
  47.   
  48.     private String one() {  
  49.         //這裏創建個對象,是爲了得到json數據,實際中json數據可能是由網絡請求得到等  
  50.         ToJsonBeanOne toJsonBeanOne = new ToJsonBeanOne(1"小熊"21);  
  51.         String jsonString = gson.toJson(toJsonBeanOne);  
  52.       
  53.         //將json數據轉換爲對象  
  54.         ToJsonBeanOne beanOne = gson.fromJson(jsonString, ToJsonBeanOne.class);  
  55.   
  56.         String showString = "";  
  57.         showString += "json:" + jsonString + "\n解析結果爲:\n" + beanOne.toString();  
  58.   
  59.         showString += "----------------------\n";  
  60.         return showString;  
  61.     }  
  62. }  

結果爲:



2.帶對象的對象實體類:

  1. public class ToJsonBeanTwo {  
  2.     private String school;  
  3.     private String classroom;  
  4.     private ToJsonBeanOne toJsonBeanOne;  
  5.   
  6.     public ToJsonBeanTwo(String school, String classroom,  
  7.             ToJsonBeanOne toJsonBeanOne) {  
  8.         super();  
  9.         this.school = school;  
  10.         this.classroom = classroom;  
  11.         this.toJsonBeanOne = toJsonBeanOne;  
  12.     }  
  13.   
  14.     public String getSchool() {  
  15.         return school;  
  16.     }  
  17.   
  18.     public String getClassroom() {  
  19.         return classroom;  
  20.     }  
  21.   
  22.     public ToJsonBeanOne getToJsonBeanOne() {  
  23.         return toJsonBeanOne;  
  24.     }  
  25.   
  26.     public void setSchool(String school) {  
  27.         this.school = school;  
  28.     }  
  29.   
  30.     public void setClassroom(String classroom) {  
  31.         this.classroom = classroom;  
  32.     }  
  33.   
  34.     public void setToJsonBeanOne(ToJsonBeanOne toJsonBeanOne) {  
  35.         this.toJsonBeanOne = toJsonBeanOne;  
  36.     }  
  37.   
  38.     @Override  
  39.     public String toString() {  
  40.         String resultString = "";  
  41.         resultString += "school:" + school + "\nclassroom:" + classroom  
  42.                 + "\ntoJsonBeanOne:\nid:" + toJsonBeanOne.getId() + "\nname:"  
  43.                 + toJsonBeanOne.getName() + "\nage:" + toJsonBeanOne.getAge()  
  44.                 + "\n";  
  45.         return resultString;  
  46.     }  
  47. }  

可以看出該對象中不僅有String類型數據,還有ToJsonBeanOne對象數據。

看看如何把該對象序列化:

  1. public class ToJsonTest extends Activity {  
  2.   
  3.     private TextView show;  
  4.     private Button start;  
  5.     private Gson gson;  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.   
  12.         initData();  
  13.     }  
  14.   
  15.     private void initData() {  
  16.   
  17.         gson = new Gson();  
  18.         show = (TextView) findViewById(R.id.showtext);  
  19.         start = (Button) findViewById(R.id.send);  
  20.         start.setOnClickListener(new OnClickListener() {  
  21.   
  22.             @Override  
  23.             public void onClick(View v) {  
  24.                 showData();  
  25.             }  
  26.         });  
  27.     }  
  28.   
  29.     private void showData() {  
  30.   
  31.         String resultString = "";  
  32.   
  33.         resultString += three() + "\n\n";  
  34.   
  35.         show.setText(resultString);  
  36.     }  
  37.   
  38.     private String three() {  
  39.   
  40.         ToJsonBeanTwo toJsonBeanTwo = new ToJsonBeanTwo("華軟""軟工五班",  
  41.                 new ToJsonBeanOne(1"小熊"21));  
  42.   
  43.         return gson.toJson(toJsonBeanTwo);  
  44.     }  
  45. }  

結果爲:



接下來看看如何把json數據轉換爲該對象:

  1. public class FromJsonTest extends Activity {  
  2.   
  3.     /** 
  4.      * 顯示數據的textview 
  5.      */  
  6.     private TextView show;  
  7.     /** 
  8.      * 按鈕 
  9.      */  
  10.     private Button start;  
  11.     /** 
  12.      * gson 
  13.      */  
  14.     private Gson gson;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.   
  21.         initData();  
  22.     }  
  23.   
  24.     private void initData() {  
  25.   
  26.         gson = new Gson();  
  27.         show = (TextView) findViewById(R.id.showtext);  
  28.         start = (Button) findViewById(R.id.send);  
  29.         start.setOnClickListener(new OnClickListener() {  
  30.   
  31.             @Override  
  32.             public void onClick(View v) {  
  33.                 showData();  
  34.             }  
  35.         });  
  36.   
  37.     }  
  38.   
  39.     private void showData() {  
  40.         String showString = "";  
  41.   
  42.         showString += three();  
  43.   
  44.         show.setText(showString);  
  45.   
  46.     }  
  47.   
  48.     private String three() {  
  49.   
  50.         ToJsonBeanTwo toJsonBeanTwo = new ToJsonBeanTwo("華軟""軟工五班",  
  51.                 new ToJsonBeanOne(1"小熊"21));  
  52.         String jsonString = gson.toJson(toJsonBeanTwo);  
  53.   
  54.         ToJsonBeanTwo beanTwo = gson.fromJson(jsonString, ToJsonBeanTwo.class);  
  55.   
  56.         String showString = "";  
  57.         showString += "json:" + jsonString + "\n解析後的數據:\n" + beanTwo.toString();  
  58.   
  59.         showString += "----------------------\n";  
  60.   
  61.         return showString;  
  62.     }  
  63. }  

 

結果爲:



3.既帶對象又帶List數據的對象(相當與json數據中有數組)實體類:

  1. public class ToJsonBeanThree {  
  2.     private String number;  
  3.     private ToJsonBeanTwo toJsonBeanTwo;  
  4.     private List<Book> books;  
  5.   
  6.     public ToJsonBeanThree(String number, ToJsonBeanTwo toJsonBeanTwo,  
  7.             List<Book> books) {  
  8.         super();  
  9.         this.number = number;  
  10.         this.toJsonBeanTwo = toJsonBeanTwo;  
  11.         this.books = books;  
  12.     }  
  13.   
  14.     public ToJsonBeanTwo getToJsonBeanTwo() {  
  15.         return toJsonBeanTwo;  
  16.     }  
  17.   
  18.     public List<Book> getBooks() {  
  19.         return books;  
  20.     }  
  21.   
  22.     public void setToJsonBeanTwo(ToJsonBeanTwo toJsonBeanTwo) {  
  23.         this.toJsonBeanTwo = toJsonBeanTwo;  
  24.     }  
  25.   
  26.     public void setBooks(List<Book> books) {  
  27.         this.books = books;  
  28.     }  
  29.   
  30.     public String getNumber() {  
  31.         return number;  
  32.     }  
  33.   
  34.     public void setNumber(String number) {  
  35.         this.number = number;  
  36.     }  
  37.   
  38.     @Override  
  39.     public String toString() {  
  40.   
  41.         String resultString = "";  
  42.         resultString += "number:" + number + "\n";  
  43.         ToJsonBeanOne toJsonBeanOne = toJsonBeanTwo.getToJsonBeanOne();  
  44.         resultString += "toJsonBeanTwo:\nschool:" + toJsonBeanTwo.getSchool()  
  45.                 + "\nclassroom:" + toJsonBeanTwo.getClassroom() + "\n";  
  46.         resultString += "toJsonBeanOne:\nid:" + toJsonBeanOne.getId()  
  47.                 + "\nname:" + toJsonBeanOne.getName() + "\nage:"  
  48.                 + toJsonBeanOne.getAge() + "\n";  
  49.         resultString += "books:\n";  
  50.   
  51.         for (int i = 0; i < books.size(); ++i) {  
  52.             resultString += "bookName:" + books.get(i).getBookName()  
  53.                     + "\nprice:" + books.get(i).getPrice() + "\n";  
  54.         }  
  55.   
  56.         return resultString;  
  57.     }  
  58.   
  59.     /** 
  60.      *  
  61.      * @ClassName: Book 
  62.      * @Description: 內部類 
  63.      * @author smile 
  64.      * @date 2016年5月12日 上午12:23:42 
  65.      *  
  66.      */  
  67.     public static class Book {  
  68.         private String bookName;  
  69.         private float price;  
  70.   
  71.         public Book(String bookName, float price) {  
  72.             super();  
  73.             this.bookName = bookName;  
  74.             this.price = price;  
  75.         }  
  76.   
  77.         public String getBookName() {  
  78.             return bookName;  
  79.         }  
  80.   
  81.         public float getPrice() {  
  82.             return price;  
  83.         }  
  84.   
  85.         public void setBookName(String bookName) {  
  86.             this.bookName = bookName;  
  87.         }  
  88.   
  89.         public void setPrice(float price) {  
  90.             this.price = price;  
  91.         }  
  92.   
  93.     }  
  94. }  

可以看出該對象裏不僅有String類型,還有ToJsonBeanTwo對象類型,還有List<Book>類型,Book對象我寫成內部類,如果有多個類共用則寫成外部類,這裏要注意:寫成內部類要寫爲靜態內部類,不然解析會出錯。

看看如何把該對象序列化:

  1. public class ToJsonTest extends Activity {  
  2.   
  3.     private TextView show;  
  4.     private Button start;  
  5.     private Gson gson;  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_main);  
  11.   
  12.         initData();  
  13.     }  
  14.   
  15.     private void initData() {  
  16.   
  17.         gson = new Gson();  
  18.         show = (TextView) findViewById(R.id.showtext);  
  19.         start = (Button) findViewById(R.id.send);  
  20.         start.setOnClickListener(new OnClickListener() {  
  21.   
  22.             @Override  
  23.             public void onClick(View v) {  
  24.                 showData();  
  25.             }  
  26.         });  
  27.     }  
  28.   
  29.     private void showData() {  
  30.   
  31.         String resultString = "";  
  32.   
  33.         resultString += five() + "\n\n";  
  34.   
  35.         show.setText(resultString);  
  36.     }  
  37.   
  38.     private String five() {  
  39.   
  40.         ToJsonBeanTwo toJsonBeanTwo = new ToJsonBeanTwo("華軟""軟工五班",  
  41.                 new ToJsonBeanOne(1"小熊"21));  
  42.         List<Book> books = new ArrayList<Book>();  
  43.         for (int i = 1; i < 5; ++i) {  
  44.             books.add(new Book("第" + i + "本書", 25f * i));  
  45.         }  
  46.         ToJsonBeanThree toJsonBeanThree = new ToJsonBeanThree("1",  
  47.                 toJsonBeanTwo, books);  
  48.   
  49.         return gson.toJson(toJsonBeanThree);  
  50.     }  
  51. }  

結果爲:


可以看出對象中List數據books轉換成json數據變爲數組

 

接下來看看如何把json數據轉換爲該對象:

  1. public class FromJsonTest extends Activity {  
  2.   
  3.     /** 
  4.      * 顯示數據的textview 
  5.      */  
  6.     private TextView show;  
  7.     /** 
  8.      * 按鈕 
  9.      */  
  10.     private Button start;  
  11.     /** 
  12.      * gson 
  13.      */  
  14.     private Gson gson;  
  15.   
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.   
  21.         initData();  
  22.     }  
  23.   
  24.     private void initData() {  
  25.   
  26.         gson = new Gson();  
  27.         show = (TextView) findViewById(R.id.showtext);  
  28.         start = (Button) findViewById(R.id.send);  
  29.         start.setOnClickListener(new OnClickListener() {  
  30.   
  31.             @Override  
  32.             public void onClick(View v) {  
  33.                 showData();  
  34.             }  
  35.         });  
  36.   
  37.     }  
  38.   
  39.     private void showData() {  
  40.         String showString = "";  
  41.           
  42.         showString += five();  
  43.   
  44.         show.setText(showString);  
  45.   
  46.     }  
  47.       
  48.     private String five() {  
  49.   
  50.         ToJsonBeanTwo toJsonBeanTwo = new ToJsonBeanTwo("華軟""軟工五班",  
  51.                 new ToJsonBeanOne(1"小熊"21));  
  52.         List<Book> books = new ArrayList<Book>();  
  53.         for (int i = 1; i < 5; ++i) {  
  54.             books.add(new Book("第" + i + "本書", 25f * i));  
  55.         }  
  56.         ToJsonBeanThree toJsonBeanThree = new ToJsonBeanThree("1",  
  57.                 toJsonBeanTwo, books);  
  58.   
  59.         String jsonString = gson.toJson(toJsonBeanThree);  
  60.   
  61.         ToJsonBeanThree beanThree = gson.fromJson(jsonString,  
  62.                 ToJsonBeanThree.class);  
  63.   
  64.         String showString = "";  
  65.         showString += "json:" + jsonString + "\n解析後的數據:\n"  
  66.                 + beanThree.toString();  
  67.   
  68.         showString += "----------------------\n";  
  69.         return showString;  
  70.     }  
  71. }  

結果爲:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章