PowerShell : retrieve JSON object by variable value

标签 powershell

$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/

相关文章:

powershell - 如何从列表中异步调用多个 URL

powershell - 从 PowerShell 调用的 SubInACL 输出的双倍间距

powershell - 为什么使用 .Invoke() 调用 Powershell 脚本 block 会返回一个集合?

powershell - 如何更改数组列表的值

linux - 运行powershell脚本而不添加publishsettings文件

c# - Powershell Cmdlet 缺少程序集 Google Api

powershell - 通过 PowerShell 启用对 HDInsight 群集的 RDP 访问

internet-explorer - 如何检查Internet Explorer COM对象的版本号

azure - 从 ADF Webhook 事件调用 Azure 自动化 Runbook - 失败,显示 "Cannot bind argument to parameter ' InputObject',因为它为空。”

powershell - 根据事件日志触发powershell