Android開發實戰(十八):Android Studio 優秀插件:GsonFormat

Android Studio 優秀插件系列:html

                      Android Studio 優秀插件(一):GsonFormatjson

                      Android Studio 優秀插件(二): Parcelable Code Generator

 

-------------------------------------------------------------------------------------------------------服務器

這幾天沒有活,因而乎整理了一些代碼,順便把一些一直在使用的東西也整理下,而後學習新的知識。。ide

-------------------------------------------------------------------------------------------------------post

做爲一個Android程序猿,當你看到後臺給你的json數據格式時:學習

{
    "id":123,
    "url": "http://img.donever.com/aa/bb.jpg",
    "width":500,
    "height":500,
    "likeCount":1,
    "description":"嘿嘿",
    "time":1234567890,
    "replyCount":0,
    "floorCount":0,
    "likeUserCount":5,
    "age":14,
    "name":"jack",
    "school":"beijing",
    "type":1,    
    "sax":"boy",
    "userid":1223
}

是否是要默默的建立一個類,而後一個個變量的private 一下,而後get()+set()?字體

若是一個json數據提供的屬性20+條或者30+條呢,一個個屬性去寫,還要保證字母不寫錯,大小寫也沒錯,是否是既浪費時間又浪費精力,那麼就試試使用GsonFormat插件吧gradle

 

如今學習下如何使用這個插件:ui

一、Android Studio 打開一個項目,點擊左上角 File -->Settings... 進行設置this

 

二、選擇插件Plugins , 搜索GsonFormat ,若是你沒有下載過這個插件,那麼搜索框下面會顯示「Nothing to show.Click Browse to....」

 

三、那就點擊藍色字體的 Browse 吧  ,這個時候會出現以下圖的界面,咱們只須要在左邊選中GsonFormat 而後點擊右面 綠色按鈕 "Install plugin" 就能夠了

 

四、完成了上面三個步驟,就可使用GsonFormat插件了

怎麼用呢,

(1)建立一個類文件,類名是看你需求自定義寫的

(2)快捷鍵 alt+insert ,會出現以下選擇框

(3)咱們點擊第一個選項,GsonFormat,就會出現一個新的框,

而後只須要將服務器給你的json數據的 格式複製進去 ,以下所示,點擊Ok就能夠了(注意json格式不要出錯,好比不要少了每一個屬性後面的逗號)

 

(4)最後一步,出現這麼一個框,這裏你能夠進行相應的編輯,好比吧一個屬性的類型改變,或者 去掉屬性前面的藍底白勾,讓類不具備某個屬性

 

效果類:

  1 public class People {
  2 
  3     /**
  4      * id : 123
  5      * url : http://img.donever.com/aa/bb.jpg
  6      * width : 500
  7      * height : 500
  8      * likeCount : 1
  9      * description : 嘿嘿
 10      * time : 1234567890
 11      * replyCount : 0
 12      * floorCount : 0
 13      * likeUserCount : 5
 14      * age : 14
 15      * name : jack
 16      * school : beijing
 17      * type : 1
 18      * sax : boy
 19      * userid : 1223
 20      */
 21 
 22     private int id;
 23     private String url;
 24     private int width;
 25     private int height;
 26     private int likeCount;
 27     private String description;
 28     private int time;
 29     private int replyCount;
 30     private int floorCount;
 31     private int likeUserCount;
 32     private int age;
 33     private String name;
 34     private String school;
 35     private int type;
 36     private String sax;
 37     private int userid;
 38 
 39     public static People objectFromData(String str) {
 40         Gson gson = new Gson();
 41 
 42         return new com.google.gson.Gson().fromJson(str, People.class);
 43     }
 44 
 45     public void setId(int id) {
 46         this.id = id;
 47     }
 48 
 49     public void setUrl(String url) {
 50         this.url = url;
 51     }
 52 
 53     public void setWidth(int width) {
 54         this.width = width;
 55     }
 56 
 57     public void setHeight(int height) {
 58         this.height = height;
 59     }
 60 
 61     public void setLikeCount(int likeCount) {
 62         this.likeCount = likeCount;
 63     }
 64 
 65     public void setDescription(String description) {
 66         this.description = description;
 67     }
 68 
 69     public void setTime(int time) {
 70         this.time = time;
 71     }
 72 
 73     public void setReplyCount(int replyCount) {
 74         this.replyCount = replyCount;
 75     }
 76 
 77     public void setFloorCount(int floorCount) {
 78         this.floorCount = floorCount;
 79     }
 80 
 81     public void setLikeUserCount(int likeUserCount) {
 82         this.likeUserCount = likeUserCount;
 83     }
 84 
 85     public void setAge(int age) {
 86         this.age = age;
 87     }
 88 
 89     public void setName(String name) {
 90         this.name = name;
 91     }
 92 
 93     public void setSchool(String school) {
 94         this.school = school;
 95     }
 96 
 97     public void setType(int type) {
 98         this.type = type;
 99     }
100 
101     public void setSax(String sax) {
102         this.sax = sax;
103     }
104 
105     public void setUserid(int userid) {
106         this.userid = userid;
107     }
108 
109     public int getId() {
110         return id;
111     }
112 
113     public String getUrl() {
114         return url;
115     }
116 
117     public int getWidth() {
118         return width;
119     }
120 
121     public int getHeight() {
122         return height;
123     }
124 
125     public int getLikeCount() {
126         return likeCount;
127     }
128 
129     public String getDescription() {
130         return description;
131     }
132 
133     public int getTime() {
134         return time;
135     }
136 
137     public int getReplyCount() {
138         return replyCount;
139     }
140 
141     public int getFloorCount() {
142         return floorCount;
143     }
144 
145     public int getLikeUserCount() {
146         return likeUserCount;
147     }
148 
149     public int getAge() {
150         return age;
151     }
152 
153     public String getName() {
154         return name;
155     }
156 
157     public String getSchool() {
158         return school;
159     }
160 
161     public int getType() {
162         return type;
163     }
164 
165     public String getSax() {
166         return sax;
167     }
168 
169     public int getUserid() {
170         return userid;
171     }
172 }
People

 若是要使用Gson解析的話 ,即 經過Json字符串直接獲取對應的類對象

public static People objectFromData(String str) {
        Gson gson = new Gson();
        return gson.fromJson(str, People.class);
    }

記得在build.gradle 中加上

compile 'com.google.code.gson:gson:2.4'
相關文章
相關標籤/搜索