使用PowerShell

时间:2017-08-07 22:53:56

标签: json powershell scripting

我有一个PowerShell脚本,想要访问嵌套密钥。这是我的JSON:

{
    "name": "versions.json",
    "versions": {
        "1.0.0": {
            "Component1": "1.0.0",
            "Component2": "1.0.0",
            "Component3": "1.0.0"
        },
        "2.0.0": {
            "Component1": "2.0.0",
            "Component2": "2.0.0",
            "Component3": "2.0.0"
        }
    }
}

我不确定如何访问每个版本(1.0.0和2.0.0)中的值。我知道我得到每个"版本的属性名称"使用:

($json.versions.PSobject.Properties) | ForEach-Object {
    "Data: $($_.Name)"
}

但是我如何遍历每个"版本"对象属性并查看其值,即如何检查" 1.0.0"中包含的内容?对于" 1.0.0"我希望看到

"Component1" at 1.0.0
"Component2" at 1.0.0
"Component3" at 1.0.0

1 个答案:

答案 0 :(得分:2)

versions执行与其属性值相同的操作:

$json.versions.PSobject.Properties | ForEach-Object {
    "Data: $($_.Name)"
    $_.Value.PSobject.Properties | ForEach-Object {
        '"{0}" at {1}' -f $_.Name, $_.Value
    }
}