$json = ConvertFrom-Json "{key:true}"
$key = "key"
Write-Host $json[$key]
我希望它打印为 true 但它没有。我知道
$json.key
会起作用。这能做到吗?
最佳答案
如果您知道 $json.key
会起作用,那么为什么要从点切换到方括号?
所有这些都将起作用:
$json = ConvertFrom-Json "{key:true}"
$key = "key"
Write-Host $json.$key
Write-Host $json.$($key)
Write-Host $json."$key"
关于PowerShell : retrieve JSON object by variable value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29717886/