自己倒腾代码,发现了这个需求,请各位大神不吝赐教: 不同场景 data 的数据类型不一样,所以需要根据入参的类型 resultType,解析 json(现在的入参是 AccountingResponse 有可能别的地方入参就是 FreezingResponse) 用 hutool 的 JSONUtil 或者 fastjson
多谢了~
public static void main(String[] args) throws Exception {
String jsonStr = "{\"code\":\"00000\",\"data\":{\"orderNo\":\"1681177011860111\",\"transType\":\"REAlTIME\",\"errorMessage\":\"successful\",\"errorCode\":\"00000\",\"accountingTime\":\"2023-04-11T09:36:51.879\",\"status\":\"SUCCESS\"},\"message\":\"SUCCESSFUL\"}";
BaseResponse<AccountingResponse> response = JSON.parseObject(jsonStr, new TypeReference<BaseResponse<AccountingResponse>>(){});
System.out.println(response.getData().getStatus());
BaseResponse<AccountingResponse> responseBaseResponse = gogogogo(AccountingResponse.class, jsonStr);
System.out.println(responseBaseResponse.getData().getOrderNo());
}
static public <R> BaseResponse<R> gogogogo(Class<R> resultType, String jsonStr) throws Exception {
BaseResponse<R> resultObject= null;
resultObject = JSON.parseObject(jsonStr, new TypeReference<BaseResponse<R>>(){});
System.out.println(resultObject.getData());
// TypeToken<?> type1 = TypeToken.getParameterized(BaseResponse.class,resultType);
resultObject = JSON.parseObject(jsonStr, new TypeReference<BaseResponse<R>>(){});
//!!!!! 这里出错
System.out.println(resultObject.getData().getOrderNo());
return resultObject;
}
public class AccountingResponse implements Serializable {
/**
* 订单编号
*/
public String orderNo;
/**
* 记账类型
*/
public String transType;
/**
* 记账状态
*/
public String status;
public String getOrderNo() {
return orderNo;
}
public void setOrderNo(String orderNo) {
this.orderNo = orderNo;
}
public String getTransType() {
return transType;
}
public void setTransType(String transType) {
this.transType = transType;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
public class BaseResponse<DATA> implements Serializable {
//响应码
private String code;
//响应码对应的内容
private String message;
private DATA data;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public DATA getData() {
return data;
}
public void setData(DATA data) {
this.data = data;
}
public static BaseResponse<String> success(String code, String message) {
BaseResponse res = new BaseResponse();
res.setCode(code);
res.setMessage(message);
return res;
}
public static BaseResponse<String> fail(String code) {
return of(code, null);
}
public static BaseResponse<String> fail(String code, String message) {
BaseResponse res = new BaseResponse();
res.setCode(code);
res.setMessage(message);
return res;
}
public static <T> BaseResponse<T> of(String errCode, T data) {
BaseResponse<T> res = new BaseResponse<T>();
res.setCode(errCode.getCode());
res.setMessage(errCode.getMessage());
res.setData(data);
return res;
}
public static <T> BaseResponse<T> of(String code, String message, T data) {
BaseResponse<T> res = new BaseResponse<T>();
res.setCode(code);
res.setMessage(message);
res.setData(data);
return res;
}
}
1
issakchill 2023-04-11 14:26:22 +08:00
fastjson 我记得可以根据某个字段反序列 支持泛型的
|