如何访问嵌套的 Java 哈希映射

huangapple go评论77阅读模式
英文:

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&lt;String, Object&gt; 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&lt;K,V&gt; and Type of the Status is ArrayList which has LinkedHashMap&lt;K,V&gt; 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&lt;String, Object&gt; response = new LinkedHashMap&lt;&gt;(Map.of(
&quot;sendResponse&quot;, new LinkedHashMap&lt;&gt;(Map.of(
&quot;return&quot;, new LinkedHashMap&lt;&gt;(Map.of(
&quot;SendResponse&quot;, new LinkedHashMap&lt;&gt;(Map.of(
&quot;txnId&quot;, &quot;4fa160ce-638f-4556-9313-afbef543fadd&quot;,
&quot;status&quot;, new ArrayList&lt;Object&gt;(List.of(
new LinkedHashMap&lt;&gt;(Map.of(
&quot;CommunicationStatus&quot;, new LinkedHashMap&lt;String, Object&gt;(Map.of(
&quot;type&quot;, &quot;EMAIL&quot;,
&quot;statusMessage&quot;, &quot;OK&quot;))))))))))))));
System.out.println(response);
Map&lt;?, ?&gt; sendResponse = (Map&lt;?, ?&gt;) response.get(&quot;sendResponse&quot;);
Map&lt;?, ?&gt; returnMap = (Map&lt;?, ?&gt;) sendResponse.get(&quot;return&quot;);
Map&lt;?, ?&gt; sendResponseCapitalS = (Map&lt;?, ?&gt;) returnMap.get(&quot;SendResponse&quot;);
List&lt;?&gt; status = (List&lt;?&gt;) sendResponseCapitalS.get(&quot;status&quot;);
Optional&lt;Map&lt;?, ?&gt;&gt; statusEmail = (Optional&lt;Map&lt;?, ?&gt;&gt;) status.stream()
.map(m -&gt; ((Map&lt;?, ?&gt;) m).get(&quot;CommunicationStatus&quot;))
.filter(m -&gt; ((Map&lt;?, ?&gt;) m).get(&quot;type&quot;).equals(&quot;EMAIL&quot;))
.findAny();
Optional&lt;Object&gt; statusMessage = statusEmail.map(m -&gt; m.get(&quot;statusMessage&quot;));
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&lt;?&gt; statusEmail = status.stream()
.map(m -&gt; ((Map&lt;?, ?&gt;) m).get(&quot;CommunicationStatus&quot;))
.filter(m -&gt; ((Map&lt;?, ?&gt;) m).get(&quot;type&quot;).equals(&quot;EMAIL&quot;))
.findAny();
Optional&lt;Object&gt; statusMessage = statusEmail.map(m -&gt; ((Map&lt;?, ?&gt;) m).get(&quot;statusMessage&quot;));

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.

huangapple
  • 本文由 发表于 2020年8月26日 10:17:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63589560.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定