您可以Key1A
通过查看Get-Member
.
让我们将您的 JSON 定义为变量$TestJson
:
$testJson = @"
{
"key1":{
"key1A":{
"someKey":"someValue"
},
"key1B":{
"someKey":"someValue"
}
},
"key2":{
"key2A":{
"someKey":"someValue"
},
"key2B":{
"someKey":"someValue"
}
},
"key3":{
"key3A":{
"someKey":"someValue"
},
"key3B":{
"someKey":"someValue"
}
}
}
"@
$testJson = $testJson | ConvertFrom-Json
我们正在寻找我们不知道在父节点下的Key1A
,我们可以通过查看输出来做到这一点$testJson
key1
$testJson | gm
$testJson | gm
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
Equals Method bool Equals(System.Object obj)
GetHashCode Method int GetHashCode()
GetType Method type GetType()
ToString Method string ToString()
key1 NoteProperty System.Management.Automation.PSCustomObject key1=@{key1A=; key1B=}
key2 NoteProperty System.Management.Automation.PSCustomObject key2=@{key2A=; key2B=}
key3 NoteProperty System.Management.Automation.PSCustomObject key3=@{key3A=; key3B=}
我们可以在这里看到所有节点及其子节点都列在定义选项卡中,如果 JSON 更大,我们将无法看到整个定义选项卡,因此我们可以选择以下两件事之一:
$testJson | gm | select-object "Definition"
($testJson | gm).Definition
所以如果我们想找到Key1A
我们可以做
$testJson | gm | ? {$_.Definition -imatch "key1A"}
它找到了 where key1a
is in 中的定义(不区分大小写,由-i
而不是指定-c
),这给了我们输出
TypeName: System.Management.Automation.PSCustomObject
Name MemberType Definition
---- ---------- ----------
key1 NoteProperty System.Management.Automation.PSCustomObject key1=@{key1A=; key1B=}
如您所见,父节点在哪里key1
,我们也可以使用
($testJson | gm | ? {$_.Definition -imatch "key1A"}).name
key1
并且查看key1
我们可以做的内容
$($testJson).$(($testJson | gm | ? {$_.Definition -imatch "key1A"}).name)
key1A key1B
----- -----
@{someKey=someValue} @{someKey=someValue}
和key1a
$($testJson).$(($testJson | gm | ? {$_.Definition -imatch "key1A"}).name).key1a
someKey
-------
someValue