英文:
How do I access nested hashmap java
问题
Map<String, Object> response = {
sendResponse = {
return = {
SendResponse = {
txnID = 4fa160ce-638f-4556-9313-afbef543fadd,
emailTypeID = 1020261131,
txnStatusCode = 1,
txnStatusMessage = OK,
status = [
{ CommunicationStatus = {
type = EMAIL,
statusCode = 1,
statusMessage = OK
}
}
]
}
}
}
}
// Extracting "statusMessage" from response Map
String statusMessage = ((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>)
response.get("sendResponse")).get("return")).get("SendResponse")).get("status")).get(0))
.get("CommunicationStatus")).get("statusMessage").toString();
英文:
I've a
Map<String, Object> response = {
sendResponse = {
return = {
SendResponse = {
txnID = 4fa160ce-638f-4556-9313-afbef543fadd,
emailTypeID = 1020261131,
txnStatusCode = 1,
txnStatusMessage = OK,
status = [
{ CommunicationStatus = {
type = EMAIL,
statusCode = 1,
statusMessage = OK
}
}
]
}
}
}
}
How to extract "statusMessage" from response Map. Please advise.
The runtime types of nested objects are LinkedHashMap<K,V>
and Type of the Status is ArrayList
which has LinkedHashMap<K,V>
inside.
I tried using get() method for example like below but I'm facing casting warnings.
Ex: response.get("sendResponse")).get("return")).get("SendResponse").get("status").get(0).get("CommunicationStatus").get(statusMessage);
答案1
得分: 0
好的,以下是您提供的代码的中文翻译:
它需要一点时间,好的。
Map<String, Object> response = new LinkedHashMap<>(Map.of(
"sendResponse", new LinkedHashMap<>(Map.of(
"return", new LinkedHashMap<>(Map.of(
"SendResponse", new LinkedHashMap<>(Map.of(
"txnId", "4fa160ce-638f-4556-9313-afbef543fadd",
"status", new ArrayList<Object>(List.of(
new LinkedHashMap<>(Map.of(
"CommunicationStatus", new LinkedHashMap<String, Object>(Map.of(
"type", "EMAIL",
"statusMessage", "OK"))))))))))))));
System.out.println(response);
Map<?, ?> sendResponse = (Map<?, ?>) response.get("sendResponse");
Map<?, ?> returnMap = (Map<?, ?>) sendResponse.get("return");
Map<?, ?> sendResponseCapitalS = (Map<?, ?>) returnMap.get("SendResponse");
List<?> status = (List<?>) sendResponseCapitalS.get("status");
Optional<Map<?, ?>> statusEmail = (Optional<Map<?, ?>>) status.stream()
.map(m -> ((Map<?, ?>) m).get("CommunicationStatus"))
.filter(m -> ((Map<?, ?>) m).get("type").equals("EMAIL"))
.findAny();
Optional<Object> statusMessage = statusEmail.map(m -> m.get("statusMessage"));
statusMessage.ifPresent(System.out::println);
您提供的代码已经被翻译成中文,只包含代码部分,没有额外的内容。如果您有任何其他需要帮助的问题,请随时提问。
英文:
It takes a bit alright.
Map<String, Object> response = new LinkedHashMap<>(Map.of(
"sendResponse", new LinkedHashMap<>(Map.of(
"return", new LinkedHashMap<>(Map.of(
"SendResponse", new LinkedHashMap<>(Map.of(
"txnId", "4fa160ce-638f-4556-9313-afbef543fadd",
"status", new ArrayList<Object>(List.of(
new LinkedHashMap<>(Map.of(
"CommunicationStatus", new LinkedHashMap<String, Object>(Map.of(
"type", "EMAIL",
"statusMessage", "OK"))))))))))))));
System.out.println(response);
Map<?, ?> sendResponse = (Map<?, ?>) response.get("sendResponse");
Map<?, ?> returnMap = (Map<?, ?>) sendResponse.get("return");
Map<?, ?> sendResponseCapitalS = (Map<?, ?>) returnMap.get("SendResponse");
List<?> status = (List<?>) sendResponseCapitalS.get("status");
Optional<Map<?, ?>> statusEmail = (Optional<Map<?, ?>>) status.stream()
.map(m -> ((Map<?, ?>) m).get("CommunicationStatus"))
.filter(m -> ((Map<?, ?>) m).get("type").equals("EMAIL"))
.findAny();
Optional<Object> statusMessage = statusEmail.map(m -> m.get("statusMessage"));
statusMessage.ifPresent(System.out::println);
I have left out some uninteresting bits from your map, it should make no difference. I am printing the map I have constructed for you to check that it looks like yours (only with a few bits left out). Output from the above snippet is:
> {sendResponse={return={SendResponse={txnId=4fa160ce-638f-4556-9313-afbef543fadd,status=[{CommunicationStatus={statusMessage=OK, type=EMAIL}}]}}}}
> OK
I am using a stream operation to take out the map from the list where type is EMAIL
. Please check whether this is correct for your requirements.
Edit: You and I have both observed the warning Type safety: Unchecked cast from Optional<capture#16-of ?> to Optional<Map<?,?>>. I found no good way of getting rid of it. The following variant of the last two lines before the final printout do not show the warning:
Optional<?> statusEmail = status.stream()
.map(m -> ((Map<?, ?>) m).get("CommunicationStatus"))
.filter(m -> ((Map<?, ?>) m).get("type").equals("EMAIL"))
.findAny();
Optional<Object> statusMessage = statusEmail.map(m -> ((Map<?, ?>) m).get("statusMessage"));
However, they give less information about the type of statusEmail
, which I find a clear disadvantage, which is why I opted for posting the first variant above in spite of the warning.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论