当网络请求返回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);