public classGsonUtil implements ParameterizedType {
publicTdeal(String response) {
try{
Type gsonType =this;
BaseStringResponse baseStringResponse =newGson().fromJson(response,gsonType);
returnbaseStringResponse.getData();
}catch(Exception e) {
e.printStackTrace();
}
return null;
}
@Override
publicType[]getActualTypeArguments() {
Class clz =this.getClass();
Type superclass = clz.getGenericSuperclass();
if(superclassinstanceofClass) {
throw newRuntimeException("Missing type parameter.");
}
ParameterizedType parameterized = (ParameterizedType) superclass;
returnparameterized.getActualTypeArguments();
}
@Override
publicTypegetOwnerType() {
return null;
}
@Override
publicTypegetRawType() {
returnBaseStringResponse.class;
}
}
调用 :List resultList =newGsonResponsePasare>() {
}.deal("{\"status\":-4,\"data\":[{\"name\":\"xiaoxuan948\"},{\"name\":\"coca\"}]}");
注意这里的大括号