有好多个嵌套bean的字符串解析,关键代码:
OutRespVO outResp = JSONObject.parseObject(resp,new TypeReference<OutRespVO<RespVO>>(){});
更新:
用hutoll的JSONUtil.toBean()解析出来的是对象,
用alibaba的JSONObject.parseObject()解析出来的list里的对象还是json,
map会转换失败,所以总结:还是用JSONUtil.toBean()比较好
OutRespVO outResp = JSONUtil.toBean(resp,new TypeReference<OutRespVO<RespVO>>(){});
String resp = requestPost(url, req, 3);
OutRespVO<RespVO> outResp = JSONObject.parseObject(resp,new TypeReference<OutRespVO<RespVO>>(){});
// 用hutoll的JSONUtil.toBean()解析出来的是对象,用alibaba的JSONObject.parseObject()解析出来的list里的对象还是json, map会转换失败
List<PriceDTO> expressFeeList = JSONUtil.toBean(FeenEntity.getFeeJson(), new TypeReference<List<PriceDTO>>() {},true);
// 这里如果用的是JSONObject.parseObject,map转换会报错,debug可以看到解析出来的是json串
Map<Long, BigDecimal> expressFeeMap = Optional.ofNullable(expressFeeList).orElse(new ArrayList<>()).stream()
.filter(t->t.getCtnId()!=null && t.getPrice()!=null)
.collect(Collectors.toMap(PriceDTO::getCtnId, PriceDTO::getPrice));