json - 只需从 Json 文件中获取一些内容
问题描述
我只需要从我的 JSON 内容中获取一些信息,但是正常情况下select-object
,where-object
我的 PowerShell 提示符什么也没有。
我做什么:
我从网页获得 JSON 输出,然后只需要 .Content。
$get_all_attributes = $result.Content | Out-String | ConvertFrom-Json | Select Attributes
当要求 PowerShell 给我一个特定对象时,$get_all_attributes.Attributes.Slot1
一切都很好。
但是现在我需要在没有Bif
(例如Slot1
但不是)的情况下获取所有插槽(Slot1 - SlotX Slot1Bif
)。之后我喜欢找到所有残疾人。但现在我什至确实得到了插槽。
我以某种方式将它从字符串转换为 Json,但现在我有点卡住了。
好看的 JSON
{
"Attributes": {
"AcPwrRcvry": "Last",
"AcPwrRcvryDelay": "Immediate",
"AesNi": "Enabled",
"AssetTag": "",
"BootMode": "Uefi",
"BootSeqRetry": "Enabled",
"CollaborativeCpuPerfCtrl": "Disabled",
"ConTermType": "Vt100Vt220",
"ControlledTurbo": "Disabled",
"Slot1": "Enabled",
"Slot1Bif": "DefaultBifurcation",
"Slot2": "Enabled",
"Slot2Bif": "DefaultBifurcation",
"Slot3": "Enabled",
"Slot3Bif": "DefaultBifurcation",
"Slot4": "Enabled",
"Slot4Bif": "DefaultBifurcation",
"Slot5": "Enabled",
"Slot5Bif": "DefaultBifurcation",
"Slot6": "Enabled",
"Slot6Bif": "DefaultBifurcation",
"Slot7": "Enabled",
"Slot7Bif": "DefaultBifurcation"
}
}
我转换的东西
$get_all_attributes | FL
Attributes : @{AcPwrRcvry=Last; AcPwrRcvryDelay=Immediate; AesNi=Enabled; AssetTag=; BootMode=Uefi; BootSeqRetry=Enabled; CollaborativeCpuPerfCtrl=Disabled;
ConTermType=Vt100Vt220; ControlledTurbo=Disabled; CorrEccSmi=Enabled; CpuInterconnectBusLinkPower=Enabled; CurrentEmbVideoState=Enabled;
DcuIpPrefetcher=Enabled;Slot1=Enabled; Slot1Bif=DefaultBifurcation; Slot2=Enabled; Slot2Bif=DefaultBifurcation; Slot3=Enabled; Slot3Bif=DefaultBifurcation; Slot4=Enabled;
Slot4Bif=DefaultBifurcation; Slot5=Enabled; Slot5Bif=DefaultBifurcation; Slot6=Enabled; Slot6Bif=DefaultBifurcation; Slot7=Enabled;
Slot7Bif=DefaultBifurcation}
解决方案
您快到了,只需使用开关“ExpandProperty”。
$get_all_attributes = $result.Content | Out-String | ConvertFrom-Json | Select -ExpandProperty Attributes
之后,最简单的方法是简单地选择您感兴趣的属性来获取所有字段......
$get_all_attributes.Attributes.BootSeqRetry
...或者对特定的子属性进行更细化:
$get_all_attributes.Attributes.BootSeqRetry
(在这种情况下,它返回Enabled
)
推荐阅读
- python - 在 NumPy 中,如何在不使用 np.s_ 的情况下从一维数组中提取范围?
- python - file.write() 没有写入文本文件
- vim - 为什么我的 Vim .vimrc 设置在 Windows 上不起作用
- kubernetes - Rancher Custom vs Imported Providor API 问题
- python - Python滚动期回报
- spring-aop - SpringAOP未绑定切入点参数错误
- ignite - Apache Ignite - 如何关闭客户端
- stata - Beta分布alpha和beta的Stata识别
- css - 在Angular9中水平对齐两个组件
- flutter - 如何管理子 BLoC 的实例