ObjectMapper 映射未正确读取 List<Map> 的数据
ObjectMapper mapping not reading data properly for List<Map>
我有一个DataResponseDto.json
{
"data": [
{
"customRule": {
"code": null,
"executionType": "ON_SUCCESS",
"description": "Description",
"owners": null,
"type": "TWO",
"enabled": true,
"objectType": "TEST",
"syncObjectKPIs": null,
"inactive": false,
"responsible": null,
"id": "0AB58A47D3A64B56A6B74DA0E66935DD",
"embedded": true,
"value": null,
"variables": [],
"kafkaEventName": null,
"lastChanged": 1530091858490,
"createPerson": null,
"externalId": null,
"groups": null,
"eventType": "UPDATE",
"branches": null,
"executionOrder": null,
"createDateTime": null,
"cronExpression": null,
"udfMetaGroups": null,
"name": "Sample1",
"location": null,
"permissionsType": "USER",
"udfValues": null,
"conditions": null,
"actions": [
{
"name": "ChecklistInstance",
"parameters": {
"templateName": "checklist"
}
}
],
"syncStatus": "IN_CLOUD",
"executionLog": []
},
"customRule": {
"code": null,
"executionType": "ON_SUCCESS",
"description": "Description",
"owners": null,
"type": "TWO",
"enabled": true,
"objectType": "TEST",
"syncObjectKPIs": null,
"inactive": false,
"responsible": null,
"id": "5033296D138C45C385AC141E1157B4FE",
"embedded": true,
"value": null,
"variables": [],
"kafkaEventName": null,
"lastChanged": 1530091858490,
"createPerson": null,
"externalId": null,
"groups": null,
"eventType": "UPDATE",
"branches": null,
"executionOrder": null,
"createDateTime": null,
"cronExpression": null,
"udfMetaGroups": null,
"name": "Sample2",
"location": null,
"permissionsType": "USER",
"udfValues": null,
"conditions": null,
"actions": [
{
"name": "ChecklistInstance",
"parameters": {
"templateName": "checklist"
}
}
],
"syncStatus": "IN_CLOUD",
"executionLog": []
}
}],
"pageSize": 1,
"currentPage": 0,
"lastPage": 0,
"totalObjectCount": 1,
"truncated": false
}
我有一个 class 来映射这个 JSON 文件。
public class DataResponseDto {
private List<Map> data;
private Integer pageSize;
private Integer currentPage;
private Integer lastPage;
private Long totalObjectCount;
private Boolean truncated;
// getter setter
...
}
现在我正在使用 ObjectMapper 将此 JSON 解析为 java class。它检索到数据中只有 1 个映射的 DataResponseDto。数据里应该是2张图。
类加载器class加载器=ClassLoader.getSystemClassLoader();
文件 file = new File(classLoader.getResource("mock/DataResponseDto.json").getFile());
ObjectMapper mapper = new ObjectMapper();
DataResponseDto dataResponseDto = mapper.readValue(file, DataResponseDto.class);
List<RuleDto> rules = dataResponseDto.getData().stream().map(m -> mapper.convertValue(m.get("customRule"), RuleDto.class)).collect(Collectors.toList());
我得到 rules.size() = 1,应该是 2
您有重复的密钥 customRule
。尝试使 customRules
具有规则的数组并重新 运行 代码。
你的问题是 private List<Map> data;
地图不能有重复的键,考虑使用其他东西,例如来自 apache commons (org.apache.commons.collections.map.MultiKeyMap
) 的 MultiKeyMap
。
我有一个DataResponseDto.json
{
"data": [
{
"customRule": {
"code": null,
"executionType": "ON_SUCCESS",
"description": "Description",
"owners": null,
"type": "TWO",
"enabled": true,
"objectType": "TEST",
"syncObjectKPIs": null,
"inactive": false,
"responsible": null,
"id": "0AB58A47D3A64B56A6B74DA0E66935DD",
"embedded": true,
"value": null,
"variables": [],
"kafkaEventName": null,
"lastChanged": 1530091858490,
"createPerson": null,
"externalId": null,
"groups": null,
"eventType": "UPDATE",
"branches": null,
"executionOrder": null,
"createDateTime": null,
"cronExpression": null,
"udfMetaGroups": null,
"name": "Sample1",
"location": null,
"permissionsType": "USER",
"udfValues": null,
"conditions": null,
"actions": [
{
"name": "ChecklistInstance",
"parameters": {
"templateName": "checklist"
}
}
],
"syncStatus": "IN_CLOUD",
"executionLog": []
},
"customRule": {
"code": null,
"executionType": "ON_SUCCESS",
"description": "Description",
"owners": null,
"type": "TWO",
"enabled": true,
"objectType": "TEST",
"syncObjectKPIs": null,
"inactive": false,
"responsible": null,
"id": "5033296D138C45C385AC141E1157B4FE",
"embedded": true,
"value": null,
"variables": [],
"kafkaEventName": null,
"lastChanged": 1530091858490,
"createPerson": null,
"externalId": null,
"groups": null,
"eventType": "UPDATE",
"branches": null,
"executionOrder": null,
"createDateTime": null,
"cronExpression": null,
"udfMetaGroups": null,
"name": "Sample2",
"location": null,
"permissionsType": "USER",
"udfValues": null,
"conditions": null,
"actions": [
{
"name": "ChecklistInstance",
"parameters": {
"templateName": "checklist"
}
}
],
"syncStatus": "IN_CLOUD",
"executionLog": []
}
}],
"pageSize": 1,
"currentPage": 0,
"lastPage": 0,
"totalObjectCount": 1,
"truncated": false
}
我有一个 class 来映射这个 JSON 文件。
public class DataResponseDto {
private List<Map> data;
private Integer pageSize;
private Integer currentPage;
private Integer lastPage;
private Long totalObjectCount;
private Boolean truncated;
// getter setter
...
}
现在我正在使用 ObjectMapper 将此 JSON 解析为 java class。它检索到数据中只有 1 个映射的 DataResponseDto。数据里应该是2张图。
类加载器class加载器=ClassLoader.getSystemClassLoader(); 文件 file = new File(classLoader.getResource("mock/DataResponseDto.json").getFile());
ObjectMapper mapper = new ObjectMapper();
DataResponseDto dataResponseDto = mapper.readValue(file, DataResponseDto.class);
List<RuleDto> rules = dataResponseDto.getData().stream().map(m -> mapper.convertValue(m.get("customRule"), RuleDto.class)).collect(Collectors.toList());
我得到 rules.size() = 1,应该是 2
您有重复的密钥 customRule
。尝试使 customRules
具有规则的数组并重新 运行 代码。
你的问题是 private List<Map> data;
地图不能有重复的键,考虑使用其他东西,例如来自 apache commons (org.apache.commons.collections.map.MultiKeyMap
) 的 MultiKeyMap
。