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

线程“main”java.lang.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"
        }]
    }
}]

我正在尝试读取deptName,如下所示

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>>>() {
        });
    }
}
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;

}

线程“main”java.lang.ClassCastException中的异常:java.util.LinkedHashMap无法强制转换为com.DeptDetail

共有1个答案

端木狐若
2023-03-14

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

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>>>() {
    });
}
}
 类似资料: