数据反序列化参数TypeReference

发布时间 2024-01-05 14:27:51作者: 品书读茶

使用jackson或者fastjson 将json字符串反序列化成对象(以fastjson为例)

TypeReference<AjaxResult<List<Person>>> typeReference = new TypeReference<AjaxResult<List<Person>>>() {};
String json = "{\"code\":200, \"success\":true, \"msg\":\"成功\",\"data\":[{\"name\":\"zhangsan\",\"address\":\"山东青岛\"},{\"name\":\"lisi\",\"address\":\"山东济宁\"}]}";
AjaxResult<List<Person>> listAjaxResult = JSONObject.parseObject(json, typeReference);
System.out.println(listAjaxResult);

restTemplate

restTemplate.exchange() 中ParameterizedTypeReference参数类似