1. 程式人生 > >Android Studio 優秀插件:GsonFormat

Android Studio 優秀插件:GsonFormat

ring oid gin file 復制 mda json 界面 gettime

作為一個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