当前位置: 首页 > 知识库问答 >
问题:

线程“main”java中出现异常。ClassCastException:java。util。LinkedHashMap无法强制转换为自定义对象

艾嘉石
2023-03-14

我有一个JSON,如下所示

[{
    "empId": 22,
    "Active": true,
    "dept": {
        "deptID": 507,
        "deptDetails": [{
            "deptName": "CSE",
            "CreateDate": "2010-11-15T15:27:45.263"
        }]
    }
}]

我正在尝试阅读下面显示的部门名称

public class TestEmp {

    public static void main(String[] args) throws IOException {
    
        
        
        String json = "[{\r\n" + 
                "    \"empId\": 22,\r\n" + 
                "    \"Active\": true,\r\n" + 
                "    \"dept\": {\r\n" + 
                "        \"deptID\": 507,\r\n" + 
                "        \"deptDetails\": [{\r\n" + 
                "            \"deptName\": \"CSE\",\r\n" + 
                "            \"CreateDate\": \"2010-11-15T15:27:45.263\"\r\n" + 
                "        }]\r\n" + 
                "    }\r\n" + 
                "}]";
    
        List<Map<String, Object>> lstMpAffOffers =  convertJsonToListMap(json);
        
        
        
        for (Map<String, Object> map : lstMpAffOffers) {
            Map<String, Object> deptMap = (Map<String, Object>) map.get("dept");
            List<DeptDetail> deptDetail = (List<DeptDetail>) deptMap.get("deptDetails");
            System.out.println(deptDetail.get(0).getDeptName());
            }
}
    
    public static List<Map<String, Object>> convertJsonToListMap(String json) throws IOException {
         ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {
        });
    }
}

部门etail.java

public class DeptDetail {
    
    
    private String deptName;
    public String getDeptName() {
        return deptName;
    }
    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }
    public String getCreateDate() {
        return CreateDate;
    }
    public void setCreateDate(String createDate) {
        CreateDate = createDate;
    }
    private String CreateDate;

}

当我试图读取deptName时,我得到了以下错误

线程“main”java中出现异常。ClassCastException:java。util。LinkedHashMap无法强制转换为com。详细信息

共有2个答案

东方文林
2023-03-14

首先,将其转换为对象数组,然后为每个元素转换为DeptDetail

Object[] objects=(Object[])deptMap.get("deptDetails");
List<DeptDetail> deptDetails = new ArrayList<DeptDetail>(); 
for (Object obj : objects) {
   DeptDetail deptDetail = (DeptDetail)obj;
   deptDetails.add(deptDetail);
}
洪捷
2023-03-14

您需要显式地将“deptDetails”对象转换为DeptDetail类的列表

public class TestEmp {

public static void main(String[] args) throws IOException {

    
    
    String json = "[{\r\n" + 
            "    \"empId\": 22,\r\n" + 
            "    \"Active\": true,\r\n" + 
            "    \"dept\": {\r\n" + 
            "        \"deptID\": 507,\r\n" + 
            "        \"deptDetails\": [{\r\n" + 
            "            \"deptName\": \"CSE\",\r\n" + 
            "            \"CreateDate\": \"2010-11-15T15:27:45.263\"\r\n" + 
            "        }]\r\n" + 
            "    }\r\n" + 
            "}]";

    List<Map<String, Object>> lstMpAffOffers =  convertJsonToListMap(json);
    
    ObjectMapper objectMapper = new ObjectMapper();
    
    for (Map<String, Object> map : lstMpAffOffers) {
        Map<String, Object> deptMap = (Map<String, Object>) map.get("dept");

        List<DeptDetail> deptDetail = objectMapper.convertValue(deptMap.get("deptDetails"), new TypeReference<List<DeptDetail>>(){})
        System.out.println(deptDetail.get(0).getDeptName());
    }
}

public static List<Map<String, Object>> convertJsonToListMap(String json) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.readValue(json, new TypeReference<List<Map<String, Object>>>() {
    });
}
}
 类似资料: