考虑以这种格式的JSON: "Stuffs": [ { "Name": "Darts", "Type": "Fun Stuff" }, { "Name": "Clean Toilet", "Type": "Boring Stuff" }] 在PowerShell 3中,我们可以获得一个Stuffs列表: $JSON = Get-Content $jsonConfigFile | Out-S
"Stuffs": [
{
"Name": "Darts",
"Type": "Fun Stuff"
},
{
"Name": "Clean Toilet",
"Type": "Boring Stuff"
}
]
在PowerShell 3中,我们可以获得一个Stuffs列表:
$JSON = Get-Content $jsonConfigFile | Out-String | ConvertFrom-Json
假设我们不知道列表的确切内容,包括对象的排序,我们如何检索具有Name字段的特定值的对象?
蛮力,我们可以遍历列表:
foreach( $Stuff in $JSON.Stuffs ) {
但我希望存在一种更直接的机制(类似于C#中的Lync或Lambda表达式).
$json = @"
{
"Stuffs":
[
{
"Name": "Darts",
"Type": "Fun Stuff"
},
{
"Name": "Clean Toilet",
"Type": "Boring Stuff"
}
]
}
"@
$x = $json | ConvertFrom-Json
$x.Stuffs[0] # access to Darts
$x.Stuffs[1] # access to Clean Toilet
$darts = $x.Stuffs | where { $_.Name -eq "Darts" } #Darts
