Android Studio 优秀插件:GsonFormat

作为一个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'

  

原文地址:https://www.cnblogs.com/foxy/p/7825380.html