简体   繁体   中英

Exception in thread “main” java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to Custom Object

I have a JSON as below

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

I am trying to read the deptName as shown below

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

DeptDetail.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;

}

When i try to read the deptName i am getting the following error

Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.DeptDetail

First, cast it into object array then cast into DeptDetail for each element

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

You need to explicitly convert the "deptDetails" object to a list of your DeptDetail class

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM