gpt4 book ai didi

json - 使用 Powershell 从 JSON 获取值

转载 作者:行者123 更新时间:2023-12-02 23:02:04 25 4
gpt4 key购买 nike

非常新手的问题。我正在尝试使用 Powershell 从 JSON 中获取某些值。具体来说,我想列出服务:TEST00000FAKE

当我运行下面的脚本时,我得到了这个:

TEST00000                                                    FAKE           
--------- ----
@{Enabled=True; Processed=2; Sent=3; Failed=4; Downloaded=5} @{Enabled=True}

我怎样才能得到只有服务的列表?

更重要的是,如何仅列出其中存在键/值 Enabled=True 的服务?

代码如下:

$JSON = '{
"Envs": {
"DEV": {
"Services": {
"TEST00000": {
"Enabled": true,
"Processed": 2,
"Sent": 3,
"Failed": 4,
"Downloaded": 5
},
"FAKE": {
"Enabled": true
}
}
}
},
"Component": {
"Digger": {
"Envs": {
"DEV": {
"DownloadE": 4
}
}
}
}
}'

$jsonobj = ConvertFrom-Json -inputObject $JSON

$jsonobj.Envs.DEV.Services

最佳答案

获取每个 Services 属性的名称。您可以像 user2734259 那样使用 Get-Member,也可以使用存储有关对象的有用信息的 psobject 属性。

$ServiceNames = $jsonobj.Envs.DEV.Services.psobject.properties.name

一旦你有了名字,你就可以遍历它们并过滤子属性 Enabled

$jsonobj.Envs.DEV.Services.psobject.properties.name | ForEach-Object { 
$_ | Where-Object {$jsonobj.Envs.DEV.Services.$_.Enabled -eq $True}
}

关于json - 使用 Powershell 从 JSON 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47944727/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com