gson 解析泛型 数据处理 BaseClass<T>

APP网络请求一般返回 json数据 如下:

{
   "id":2200,
    "status":1,
    "member":2,
    "usreid":2200,
    "alias":"QQ 804031885",
    "name":"张风英",
    "sex":1,
    "msg":"登录成功欢迎回来"
}

这种数据比较好处理 创建一个实体类 Module.class

public class Module {
    private int id;
    private int status;
    private int member;
    private int usreid;
    private String alias;
    private String name;
    private int sex;
    private String msg;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getMember() {
        return member;
    }

    public void setMember(int member) {
        this.member = member;
    }

    public int getUsreid() {
        return usreid;
    }

    public void setUsreid(int usreid) {
        this.usreid = usreid;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(String alias) {
        this.alias = alias;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }
}

 json转为实体类对像

Module module= new Gson().fromJson(json,Module.class);

这样就能得到我们需要的对像内容。

 

有的返回json多种数据格式 我们不可能每一种数据写一个请求的接口数据, 所以需要使用到具体的泛型类来处理数据了。 如下返回的JSON

{
    "total":30,
    "per_page":20,
    "current_page":1,
    "last_page":2,
    "data":[
        {
            "id":42,
            "title":"\u5348\u591cDj",
            "tag":"\u5348\u591cDj",
            "picture":"",
            "url":"",
            "type":0,
            "status":1,
            "datetime":"1623428508"
        },
        {
            "id":40,
            "title":"\u670b\u53cb\u7684\u9152 Dj - DJ\u821e\u66f2",
            "tag":"DJ\u821e\u66f2",
            "picture":"",
            "url":"https:\/\/mp3.9ku.com\/mp3\/470\/469902.mp3",
            "type":0,
            "status":1,
            "datetime":"1623428513"
        }
    ]
}

创建一个实体类misc.class

public class misc implements Serializable {
    private String id;
    private String title;
    private String tag;
    private String url;
    private String picture;
    private int type;
    private int status;
    private String datetime;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getDatetime() {
        return datetime;
    }

    public void setDatetime(String datetime) {
        this.datetime = datetime;
    }
}

 然后创建一个泛型实体类解析数据  BaseClass<T> 

public class BaseClass<T>{
    private int total;
    private int per_page;
    private int current_page;
    private int last_page;
    private List<T> data;

    public int getTotal() {
        return total;
    }

    public void setTotal(int total) {
        this.total = total;
    }

    public int getPer_page() {
        return per_page;
    }

    public void setPer_page(int per_page) {
        this.per_page = per_page;
    }

    public int getCurrent_page() {
        return current_page;
    }

    public void setCurrent_page(int current_page) {
        this.current_page = current_page;
    }

    public int getLast_page() {
        return last_page;
    }

    public void setLast_page(int last_page) {
        this.last_page = last_page;
    }

    public List<T> getData() {
        return data;
    }

    public void setData(List<T> data) {
        this.data = data;
    }
}

 通过下面的代码轻松获取到我们所需要的对像

Type objectType = new TypeToken<BaseClass<misc>>(){}.getType();
BaseClass  baseClass = new Gson().fromJson(date, objectType);
List<misc> data = baseClass.getData();

拿到对像数组了,下面就可以实现自己的业务逻辑了。

 

上一篇:Google的Gson——GsonUtil工具类


下一篇:Android开发中混淆后Gson序列化、反序列化的那些坑