github大佬地址
使用
//class 可以传参DataBean.class
public static <T> T handlerJson(String json, Class<T> tClass) {
T resultData = null;
if (CommonUtils.StringNotNull(json) && !nullString.equals(json)) {
if (isArray(json)) {
resultData = BaseNetUtil.parseFromJson(json,
TypeBuilder.newInstance(ArrayList.class).addTypeParam(tClass).build());
} else if (isObj(json)) {
resultData = BaseNetUtil.parseFromJson(json, TypeBuilder.newInstance(tClass).build());
}
}
return resultData;
}
public ResultData<T> parseNetworkResponse(Response response, int id) throws Exception {
//s 是JsonObject
ResultData<T> resultData = BaseNetUtil.parseFromJson(s.toString(), new TypeToken<ResultData<T>>() {
}.getType());
}
public static <T> T parseFromJson(String json, Type type) {
try {
return !TextUtils.isEmpty(json) && type != null ? (T) new Gson().fromJson(json, type) : null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}