使用Powershell从JSON获取值

编程入门 行业动态 更新时间:2024-10-25 02:26:34
本文介绍了使用Powershell从JSON获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

非常初学者的问题.我正在尝试使用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获取值

本文发布于:2023-10-29 08:22:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539196.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Powershell   JSON

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!