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