Android Studio 優秀外掛程式:GsonFormat

來源:互聯網
上載者:User

標籤:步驟   one   this   mpi   jin   return   etl   eth   obj   

作為一個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外掛程式吧

 

現在學習下如何使用這個外掛程式:

1、Android Studio 開啟一個項目,點擊左上方 File -->Settings... 進行設定

 

2、選擇外掛程式Plugins , 搜尋GsonFormat ,如果你沒有下載過這個外掛程式,那麼搜尋方塊下面會顯示“Nothing to show.Click Browse to....”

 

3、那就點擊藍色字型的 Browse 吧  ,這個時候會出現如的介面,我們只需要在左邊選中GsonFormat 然後點擊右面 綠色按鈕 "Install plugin" 就可以了

 

4、完成了上面三個步驟,就可以使用GsonFormat外掛程式了

怎麼用呢,

(1)建立一個類檔案,類名是看你需求自訂寫的

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

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

然後只需要將伺服器給你的json資料的 格式複製進去 ,如下所示,點擊Ok就可以了(注意json格式不要出錯,比如不要少了每個屬性後面的逗號)

 

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

 

效果類:

public class People {    /**     * 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 int id;    private String url;    private int width;    private int height;    private int likeCount;    private String description;    private int time;    private int replyCount;    private int floorCount;    private int likeUserCount;    private int age;    private String name;    private String school;    private int type;    private String sax;    private int userid;    public static People objectFromData(String str) {        Gson gson = new Gson();        return new com.google.gson.Gson().fromJson(str, People.class);    }    public void setId(int id) {        this.id = id;    }    public void setUrl(String url) {        this.url = url;    }    public void setWidth(int width) {        this.width = width;    }    public void setHeight(int height) {        this.height = height;    }    public void setLikeCount(int likeCount) {        this.likeCount = likeCount;    }    public void setDescription(String description) {        this.description = description;    }    public void setTime(int time) {        this.time = time;    }    public void setReplyCount(int replyCount) {        this.replyCount = replyCount;    }    public void setFloorCount(int floorCount) {        this.floorCount = floorCount;    }    public void setLikeUserCount(int likeUserCount) {        this.likeUserCount = likeUserCount;    }    public void setAge(int age) {        this.age = age;    }    public void setName(String name) {        this.name = name;    }    public void setSchool(String school) {        this.school = school;    }    public void setType(int type) {        this.type = type;    }    public void setSax(String sax) {        this.sax = sax;    }    public void setUserid(int userid) {        this.userid = userid;    }    public int getId() {        return id;    }    public String getUrl() {        return url;    }    public int getWidth() {        return width;    }    public int getHeight() {        return height;    }    public int getLikeCount() {        return likeCount;    }    public String getDescription() {        return description;    }    public int getTime() {        return time;    }    public int getReplyCount() {        return replyCount;    }    public int getFloorCount() {        return floorCount;    }    public int getLikeUserCount() {        return likeUserCount;    }    public int getAge() {        return age;    }    public String getName() {        return name;    }    public String getSchool() {        return school;    }    public int getType() {        return type;    }    public String getSax() {        return sax;    }    public int getUserid() {        return userid;    }}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‘

  

Android Studio 優秀外掛程式:GsonFormat

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.