一、json轉(zhuǎn)對(duì)象:
應(yīng)用場(chǎng)景:接口中一個(gè)數(shù)據(jù)可能為對(duì)象叠洗,也可能是一個(gè)對(duì)象List(此處其實(shí)接口規(guī)范后,完全用不到下邊的方法权逗,沒辦法罐呼,一下方法就是想試試能不能適配這個(gè)情況)
public class HttpBeen<T> implements Serializable{
private String result;
private String errorCode;
private T ResultData;
}
對(duì)象中采用<T>泛型修飾。以下是將泛型轉(zhuǎn)換為對(duì)象的方法:
/** 對(duì)象類型轉(zhuǎn)換
* @param obj 源對(duì)象
* @param cla 目標(biāo)對(duì)象類型
* @param <T> 泛型
* @return 目標(biāo)對(duì)象
*/
public static <T> T jsonToBean(LinkedTreeMap obj,Class<T> cla) {
T dataBean = null;
try {
if(null==obj) {
return null;
}
dataBean = cla.newInstance();
Field[] beanFields = cla.getDeclaredFields();
//通過set方法給目標(biāo)對(duì)象賦值
for(Field field:beanFields){
if (field.getModifiers() == 26) continue;
field.setAccessible(true);
String fieldName = field.getName();
Class<?> fieldType = field.getType();
String fieldValue = obj.get(fieldName)==null?null:obj.get(fieldName).toString();
if(fieldValue!=null){
try {
if(String.class.equals(fieldType)){
field.set(dataBean, fieldValue);
}else if(byte.class.equals(fieldType)){
field.setByte(dataBean, Byte.parseByte(fieldValue));
}else if(Byte.class.equals(fieldType)){
field.set(dataBean, Byte.valueOf(fieldValue));
}else if(boolean.class.equals(fieldType)){
field.setBoolean(dataBean, Boolean.parseBoolean(fieldValue));
}else if(Boolean.class.equals(fieldType)){
field.set(dataBean, Boolean.valueOf(fieldValue));
}else if(short.class.equals(fieldType)){
field.setShort(dataBean, Short.parseShort(fieldValue));
}else if(Short.class.equals(fieldType)){
field.set(dataBean, Short.valueOf(fieldValue));
}else if(int.class.equals(fieldType)){
field.setInt(dataBean, Integer.parseInt(fieldValue));
}else if(Integer.class.equals(fieldType)){
field.set(dataBean, Integer.valueOf(fieldValue));
}else if(long.class.equals(fieldType)){
field.setLong(dataBean, Long.parseLong(fieldValue));
}else if(Long.class.equals(fieldType)){
field.set(dataBean, Long.valueOf(fieldValue));
}else if(float.class.equals(fieldType)){
field.setFloat(dataBean, Float.parseFloat(fieldValue));
}else if(Float.class.equals(fieldType)){
field.set(dataBean, Float.valueOf(fieldValue));
}else if(double.class.equals(fieldType)){
field.setDouble(dataBean, Double.parseDouble(fieldValue));
}else if(Double.class.equals(fieldType)){
field.set(dataBean, Double.valueOf(fieldValue));
}else if(Date.class.equals(fieldType)){
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
field.set(dataBean, sdf.parse(fieldValue));
}
} catch (IllegalArgumentException e) {
// e.printStackTrace();
return null;
} catch (ParseException e) {
// e.printStackTrace();
return null;
}
}
}
} catch (Exception e) {
return null;
// e.printStackTrace();
}
return dataBean;
}
此處LinkedTreeMap的原因是項(xiàng)目采用Retrofit+OKHttp的方式網(wǎng)絡(luò)框架继控,Gson解析械馆,泛型情況下Gson會(huì)將格式擦除,轉(zhuǎn)換為L(zhǎng)inkedTreeMap武通。
二霹崎、json轉(zhuǎn)對(duì)象List:
/**
* 對(duì)象數(shù)組類型轉(zhuǎn)換
* @param json 源對(duì)象
* @param clas 目標(biāo)對(duì)象類型
* @param <T> 泛型
* @return 目標(biāo)對(duì)象
*/
public static <T> List<T> jsonToBeanList(String json, Type clas) {
Gson gson = new Gson();
List<T> list = new ArrayList<>();
JsonParser parser = new JsonParser();
JsonArray jsonarray = parser.parse(json).getAsJsonArray();
for (JsonElement element : jsonarray) {
list.add(gson.fromJson(element, clas));
}
return list;
}
這個(gè)沒啥說的,自己百度看解析吧冶忱。
以上方法借鑒了一些大神的方法思路