使用Powershell从JSON获取值 [英] Get values from JSON with Powershell
问题描述
非常初学者的问题.我正在尝试使用Powershell从JSON中获取某些值.特别是,我只想列出服务: TEST00000 和 FAKE .
Very beginner question. I'm trying to get certain values from JSON with Powershell.. Specifically, I want to list the Services: TEST00000 and FAKE only.
当我运行下面的脚本时,我得到了:
When I run the script below, I get this:
TEST00000 FAKE
--------- ----
@{Enabled=True; Processed=2; Sent=3; Failed=4; Downloaded=5} @{Enabled=True}
如何获取仅服务列表?
更重要的是,如何仅列出其中包含键/值 Enabled = True 的服务?
More importantly, how do I list only the services which has the key/value Enabled=True present inside of them?
这是代码:
$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
属性来存储有关对象的有用信息.
To get the name of each Services
property. You can use Get-Member
like user2734259 did or you can use the psobject
property which stores useful information about an object.
$ServiceNames = $jsonobj.Envs.DEV.Services.psobject.properties.name
一旦有了名称,就可以在它们上循环并根据子属性Enabled
Once you have the names you can loop over them and filter on the sub-property Enabled
$jsonobj.Envs.DEV.Services.psobject.properties.name | ForEach-Object {
$_ | Where-Object {$jsonobj.Envs.DEV.Services.$_.Enabled -eq $True}
}
这篇关于使用Powershell从JSON获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!