PowerShell 中的 HashMap

HashMap in PowerShell

我在 PowerShell 中实现 HashMap 时遇到问题。我使用

从 ReST 响应创建了 HasMap
$response_connection_hashmap = $response_connection|foreach {
@{  $_.name = $_.id }
}

我正在使用

成功验证 hasmap
$response_connection_hashmap.GetEnumerator()|Sort-Object Name

然而,在按键搜索值时,我使用以下

$response_connection_hashmap.Item("Key01")

低于错误

Exception getting "Item": "Cannot convert argument "index", with value: 
"Key01", for "get_Item" to type "System.Int32": "Cannot convert value 
"Key01" to type "System.Int32". Error: "Input string was not in a correct 
format."""

您正在生成哈希表数组:

$response_connection_hashmap.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array

试试这个:

$response_connection_hashmap = @{}
$response_connection|foreach { $response_connection_hashmap.add($_.name, $_.id) }