我需要将列表
[{
"OPERATION_NAME": "COPY",
"OPERATION_STATUS": "READY",
"COUNT(1)": 1
}, {
"OPERATION_NAME": "ARCHIVE",
"OPERATION_STATUS": "READY",
"COUNT(1)": 1
}, {
"OPERATION_NAME": "EXPORT_CA",
"OPERATION_STATUS": "READY",
"COUNT(1)": 1
}, {
"OPERATION_NAME": "MERGE",
"OPERATION_STATUS": "READY",
"COUNT(1)": 1
}, {
"OPERATION_NAME": "MERGE",
"OPERATION_STATUS": "DONE",
"COUNT(1)": 1
}]
我将其更改为:
[{
"READY": 0,
"NEW": 0,
"DONE": 0,
"OPERATION_NAME": "MERGE"
}, {
"READY": 0,
"NEW": 0,
"DONE": 0,
"OPERATION_NAME": "ARCHIVE"
}, {
"READY": 0,
"NEW": 0,
"DONE": 0,
"OPERATION_NAME": "COPY"
}, {
"READY": 0,
"NEW": 0,
"DONE": 0,
"OPERATION_NAME": "EXPORT_CA"
}]
但是我无法获取COUNT(1)的值并将其设置为我正在创建的新对象。
最后我应该有这个:
[{
"READY": 1,
"NEW": 0,
"DONE": 1,
"OPERATION_NAME": "MERGE"
}, {
"READY": 1,
"NEW": 0,
"DONE": 0,
"OPERATION_NAME": "ARCHIVE"
}, {
"READY": 1,
"NEW": 0,
"DONE": 0,
"OPERATION_NAME": "COPY"
}, {
"READY": 1,
"NEW": 0,
"DONE": 0,
"OPERATION_NAME": "EXPORT_CA"
}]
这是我的功能,可以将一个对象转换为另一个对象:
private List<Map<String, Object>> transformObject(List<Map<String, Object>> resDB) {
Gson gson = new Gson();
LOGGER.info("Json response : {}", gson.toJson(resDB));
List<Map<String, Object>> result = new ArrayList<>();
List<String> listOfOperationName = new ArrayList<>();
List<String> listOfOperationStatus = new ArrayList<>();
// Retrieve OP Name and OP Status
for (Map<String, Object> map : resDB) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
if(key.equals("OPERATION_NAME")) {
listOfOperationName.add(value.toString());
} else if(key.equals("OPERATION_STATUS")) {
listOfOperationStatus.add(value.toString());
}
}
}
// Remove duplicates
Set<String> setName = new HashSet<>(listOfOperationName);
listOfOperationName.clear();
listOfOperationName.addAll(setName);
// Remove duplicates
Set<String> setStatus = new HashSet<>(listOfOperationStatus);
listOfOperationStatus.clear();
listOfOperationStatus.addAll(setStatus);
// Create field OP Name with value
for (String tempOpName : listOfOperationName) {
Map<String, Object> tempObjName = new HashMap<>();
tempObjName.put("OPERATION_NAME", tempOpName);
for (String tempOpStatus : listOfOperationStatus) {
tempObjName.put(tempOpStatus, 0);
}
result.add(tempObjName);
}
Gson gsonResult = new Gson();
LOGGER.info("Json response : {}", gsonResult.toJson(result));
return result;
}
如果您有想法如何做,那就太好了!非常感谢!
答案 0 :(得分:1)
使用这两种帮助方法
Received and deleted message: 250e37a8-d779-48a1-9941-84219a82513e
您可以轻松进行转换
private static Map<String, Object> transform(Map<String, Object> initial) {
Map<String, Object> transformed = new HashMap<>();
transformed.put("OPERATION_NAME", (String) initial.get("OPERATION_NAME"));
transformed.put("READY", 0);
transformed.put("NEW", 0);
transformed.put("DONE", 0);
transformed.put((String) initial.get("OPERATION_STATUS"),
Integer.valueOf((String) initial.get("COUNT(1)")));
return transformed;
}
private static Map<String, Object> mergeMaps(Map<String, Object> left, Map<String, Object> right) {
for (Entry<String, Object> re : right.entrySet()) {
if (!"OPERATION_NAME".equals(re.getKey())) {
left.merge(re.getKey(), re.getValue(), (l, r) -> {
Integer res = 0;
res += (Integer) l;
res += (Integer) r;
return res;
});
}
}
return left;
}
希望有帮助!