JSONObject强转对象数组不能循环遍历问题

一:问题描述

     在开发一个测试工具的时候,请求调用其他模块返回的JSONObject对象在代码里边强制转换List对象数组成功,但是在后续循环遍历的时候报java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.example.demo.bean.DepartmentInfo错误

具体代码如下:

JSONObject jsonDepat = restTemplate.getForObject(oauthUri+getDepartUrl+"?access_token="
      +access_token,JSONObject.class );
List departmentInfos = (List)jsonDepat;
for(DepartmentInfo departmentInfo : departmentInfos )
{
   departmentInfo.getTenantId();
}

二:解决办法

JSONObject jsonDepat = restTemplate.getForObject(oauthUri+getDepartUrl+"?access_token="+access_token, JSONObject.class );
String jsonStr = JSON.toJSONString( jsonDepat );
List depatList = JSONArray.parseArray( jsonStr, DepartmentInfo.class);

 

 

你可能感兴趣的:(日常报错)