相关疑难解决方法(0)

PowerShell:按字段值检索JSON对象

考虑这种格式的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表达式).

powershell json powershell-3.0

49
推荐指数
4
解决办法
11万
查看次数

标签 统计

json ×1

powershell ×1

powershell-3.0 ×1