GSON解析泛型json 封装通用类

android 专栏收录该内容
9 篇文章 0 订阅

https://www.cnblogs.com/qq78292959/p/3781808.html

根据这位大神的思路,结合现有Gson(版本2.8.5)进行泛型实体类简单封装

本文章只针对

#第一种:
{"success":true,"data":{"averageStarLevel":4.7,"remarkCount":10}}
#第二种:
{"success":true,"data":{"page":10,"pageCount":29,"list":[{"starLevel":4,"remarkCotnent":"评价方未及时做出评价,系统默认满意!","remarkTime":"2013-02-27 07:21:48","explainContent":"","postMemberId":"y**f","tpLogoURL":"http://i04.c.aliimg.com/cms/upload/2012/186/684/486681_1232736939.png"},{"starLevel":4,"remarkCotnent":"评价方未及时做出评价,系统默认满意!","remarkTime":"2013-02-27 07:21:48","explainContent":"","postMemberId":"y**f","tpLogoURL":"http://i04.c.aliimg.com/cms/upload/2012/186/684/486681_1232736939.png"}],"statistics":{"star5":20,"star4":40,"star3":30,"star2":10,"star1":0}}}

第三种未做测试 道理一样稍微修改应该可以使用

每一个{}我看作一个实体,进行不同的实体编写,实体生成我使用的

PS:自己从别的博客上整理了一下,无法通过Android Stuido 安装的可以用离线方式,

插件下载地址:https://download.csdn.net/download/qq524752841/10628516

分别生成了三个实体



public class Data
{

    private String title;
    private String url;
    private String imageUrl;
    private int type;
    private boolean isMobile;
    private boolean enabled;
    private int displayOrder;
    private int id;
    public void setTitle(String title) {
        this.title = title;
    }
    public String getTitle() {
        return title;
    }

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

    public void setImageUrl(String imageUrl) {
        this.imageUrl = imageUrl;
    }
    public String getImageUrl() {
        return imageUrl;
    }

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

    public void setIsMobile(boolean isMobile) {
        this.isMobile = isMobile;
    }
    public boolean getIsMobile() {
        return isMobile;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }
    public boolean getEnabled() {
        return enabled;
    }

    public void setDisplayOrder(int displayOrder) {
        this.displayOrder = displayOrder;
    }
    public int getDisplayOrder() {
        return displayOrder;
    }

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

}

import java.util.List;



public class Result<T> {

    private int pageNum;
    private int pageSize;
    private List<T> data;
    private int totalRecord;
    private int totalPage;
    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }
    public int getPageNum() {
        return pageNum;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getPageSize() {
        return pageSize;
    }

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

    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }
    public int getTotalRecord() {
        return totalRecord;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }
    public int getTotalPage() {
        return totalPage;
    }

}

public class JsonRootBean<T> {

    private Result<T> result;
    private String message;
    private boolean success;
    public void setResult(Result<T> result) {
        this.result = result;
    }
    public Result<T> getResult() {
        return result;
    }

    public void setMessage(String message) {
        this.message = message;
    }
    public String getMessage() {
        return message;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }
    public boolean getSuccess() {
        return success;
    }

}

三个类名可以随便定义 但是里面的字段并必须跟json的一直,不一致的情况过会再说

 

String json = "{\n" +
        "  \"result\": {\n" +
        "    \"pageNum\": 1,\n" +
        "    \"pageSize\": 5,\n" +
        "    \"data\": [\n" +
        "      {\n" +
        "        \"title\": \"tuan\",\n" +
        "        \"url\": \"/Wx/Pintuan/List\",\n" +
        "        \"imageUrl\": \"/Uploads/MobileBanner/2017/09/05/636402035800826088.jpg--400x300.png\",\n" +
        "        \"type\": 0,\n" +
        "        \"isMobile\": true,\n" +
        "        \"enabled\": true,\n" +
        "        \"displayOrder\": 0,\n" +
        "        \"id\": 9\n" +
        "      },\n" +
        "      {\n" +
        "        \"title\": \"test1\",\n" +
        "        \"url\": \"http://www.zuanjisc.com/wx/profile/activity\",\n" +
        "        \"imageUrl\": \"/Uploads/MobileBanner/2018/08/06/636691713025637935.jpg--400x300.jpg\",\n" +
        "        \"type\": 0,\n" +
        "        \"isMobile\": true,\n" +
        "        \"enabled\": true,\n" +
        "        \"displayOrder\": 0,\n" +
        "        \"id\": 1\n" +
        "      }\n" +
        "    ],\n" +
        "    \"totalRecord\": 2,\n" +
        "    \"totalPage\": 1\n" +
        "  },\n" +
        "  \"message\": \"获取数据成功\",\n" +
        "  \"success\": true\n" +
        "}";

JsonRootBean<Data> daf1 = new Gson().fromJson(json, new TypeToken<JsonRootBean<Data>>() { }.getType());

 

再说一下实体里面字段名跟对json里面不一致问题Gson提供一个注解

@SerializedName注解    能指定该字段在序列化成json时的名称

private String message;  

改成

@SerializedName(message)

private String msg;

  • 0
    点赞
  • 0
    评论
  • 2
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值