首页 > 解决方案 > 只需从 Json 文件中获取一些内容

问题描述

我只需要从我的 JSON 内容中获取一些信息,但是正常情况下select-objectwhere-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}

标签: jsonpowershell

解决方案


您快到了,只需使用开关“ExpandProperty”。

$get_all_attributes = $result.Content | Out-String | ConvertFrom-Json | Select -ExpandProperty Attributes

之后,最简单的方法是简单地选择您感兴趣的属性来获取所有字段...... $get_all_attributes.Attributes.BootSeqRetry

...或者对特定的子属性进行更细化:

$get_all_attributes.Attributes.BootSeqRetry

(在这种情况下,它返回Enabled


推荐阅读