将返回的ResponseBody转为实体类

当网络请求返回ResponseBody时,如何将ResponseBody转为实体对象?
1.如果是个无嵌套的普通的实体类,可使用如下方法:
public Object JSONTOBean(ResponseBody body, Class clazz)
{
    Object obj = null;
    try
    {
        String json = new String(body.bytes());
        Gson gson = new Gson();
        obj = gson.fromJson(json, clazz);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return obj;
}
2.如果是有嵌套的实体类,可使用如下方法:
//注意Type的包为:java.lang.reflect.Type
public Object JSONTOBean(ResponseBody body, Type type)
{
    Object obj = null;
    try
    {
        String json = new String(body.bytes());
        Gson gson = new Gson();
        obj = gson.fromJson(json, type);
    }
    catch (IOException e)
    {
        e.printStackTrace();
    }
    return obj;
}
3.完整代码:
public class ResponseBodyToBean
{
    private ResponseBodyToBean()
    {
    }

    private static class Holder
    {
        private static ResponseBodyToBean instance = new ResponseBodyToBean();
    }

    public static ResponseBodyToBean getInstance()
    {
        return Holder.instance;
    }

    public Object JSONTOBean(ResponseBody body, Class clazz)
    {
        Object obj = null;
        try
        {
            String json = new String(body.bytes());
            Gson gson = new Gson();
            obj = gson.fromJson(json, clazz);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return obj;
    }

    public Object JSONTOBean(ResponseBody body, Type type)
    {
        Object obj = null;
        try
        {
            String json = new String(body.bytes());
            Gson gson = new Gson();
            obj = gson.fromJson(json, type);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return obj;
    }
}
4.实例:将网络请求返回的ResponseBody的如下内容转为实体对象:
{
	"weatherinfo": {
		"city": "北京",
		"cityid": "101010100",
		"temp": "27.9",
		"WD": "南风",
		"WS": "小于3级",
		"SD": "28%",
		"AP": "1002hPa",
		"njd": "暂无实况",
		"WSE": "<3",
		"time": "17:55",
		"sm": "2.1",
		"isRadar": "1",
		"Radar": "JC_RADAR_AZ9010_JB"
	}
}
4.1)定义实体类对象
public class JsonRootBean 
{
    private Weatherinfo weatherinfo;
    public void setWeatherinfo(Weatherinfo weatherinfo) 
    {
        this.weatherinfo = weatherinfo;
    }
    public Weatherinfo getWeatherinfo() 
    {
         return weatherinfo;
    }
}
public class Weatherinfo
{
    private String city;
    private String cityid;
    private String temp;
    private String WD;
    private String WS;
    private String SD;
    private String AP;
    private String njd;
    private Date WSE;
    private String time;
    private String sm;
    private String isRadar;
    private String Radar;
    public void setCity(String city)
    {
        this.city = city;
    }
    public String getCity()
    {
        return city;
    }
    public void setCityid(String cityid)
    {
        this.cityid = cityid;
    }
    public String getCityid()
    {
        return cityid;
    }
    public void setTemp(String temp)
    {
        this.temp = temp;
    }
    public String getTemp()
    {
        return temp;
    }
    public void setWD(String WD)
    {
        this.WD = WD;
    }
    public String getWD()
    {
        return WD;
    }
    public void setWS(String WS)
    {
        this.WS = WS;
    }
    public String getWS()
    {
        return WS;
    }
    public void setSD(String SD)
    {
        this.SD = SD;
    }
    public String getSD()
    {
        return SD;
    }
    public void setAP(String AP)
    {
        this.AP = AP;
    }
    public String getAP()
    {
        return AP;
    }
    public void setNjd(String njd)
    {
        this.njd = njd;
    }
    public String getNjd()
    {
        return njd;
    }
    public void setWSE(Date WSE)
    {
        this.WSE = WSE;
    }
    public Date getWSE()
    {
        return WSE;
    }
    public void setTime(String time)
    {
        this.time = time;
    }
    public String getTime()
    {
        return time;
    }
    public void setSm(String sm)
    {
        this.sm = sm;
    }
    public String getSm()
    {
        return sm;
    }
    public void setIsRadar(String isRadar)
    {
        this.isRadar = isRadar;
    }
    public String getIsRadar()
    {
        return isRadar;
    }
    public void setRadar(String Radar)
    {
        this.Radar = Radar;
    }
    public String getRadar()
    {
        return Radar;
    }
}
4.2)使用
java.lang.reflect.Type type = new TypeToken<JsonRootBean>() {}.getType();
JsonRootBean bean = (JsonRootBean) ResponseBodyToBean.getInstance().JSONTOBean(result, type);
5扩展:

Gson解析list类型的json

Gson gson = new Gson();
Type type = new TypeToken<List<Object>>() {}.getType(); 
List<Object> list = gson.fromJson(string, type);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值