Powershell - 使用数组中的字符串从 JSON 获取值
Powershell - Get value from JSON using string from array
我想做的是从嵌套 JSON 中获取一个特定值。使用数组键作为表达式。
包含键和值的数组:
$AccountService = @{
'root.branch.setting1'= 'Val1'
'root.branch.setting2'= 'Val2'
'root.branch.setting3'= 'Val3'
}
创建JSON对象
$json = Get-Content 'C:\Users\ramosfer\Documents\test.json' | ConvertFrom-Json
使用循环从数组中获取每个键以从 JSON 中获取值。在 Expression ($json.root.branch.setting1)
中期待这样的事情
$AccountService.GetEnumerator() | % {
$json | Select-Object @{Name="Val"; Expression={$json.$_}}
}
使用这个 $json.$_ 并期待这样的结果
Val
---
Val1
Val2
Val3
解决嵌套属性的最佳方法是一次解决一个:)
一个更简单的例子,只检索一个值:
$json = '{"root":{"branch":{"setting1":"Value 1","setting2":"Value 2","setting3":"Value 3"}}}' |ConvertFrom-Json
# define the "path" to the value we want
$path = 'root.branch.setting1'
# split path into individual property names
$path = $path.Split('.')
# start from the root object and then start "walking" down the object hierarchy
$cursor = $json
foreach($property in $path)
{
# advance cursor one level deeper
$cursor = $cursor.$property
}
# this will now contain "Value 1"
$cursor
你可以把它变成一个简洁的小函数:
function Resolve-MemberChain
{
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[psobject[]]$InputObject,
[Parameter(Mandatory = $true, Position = 0)]
[string[]]$MemberPath,
[Parameter(Mandatory = $false)]
[string]$Delimiter
)
begin {
if($PSBoundParameters.ContainsKey('Delimiter')){
$MemberPath = $MemberPath.Split([string[]]@($Delimiter))
}
}
process {
foreach($obj in $InputObject){
$cursor = $obj
foreach($member in $MemberPath){
$cursor = $cursor.$member
}
$cursor
}
}
}
然后像这样使用它:
$json |Resolve-MemberChain 'root.branch.setting1' -Delimiter '.'
或者,如您的情况,在计算的 属性 表达式中,如下所示:
$AccountService.GetEnumerator()|%{
$path = $_.Key
$json |Select @{Name='Val';Expression={$_ |Resolve-MemberChain $path -Delimiter .}}
}
我想做的是从嵌套 JSON 中获取一个特定值。使用数组键作为表达式。
包含键和值的数组:
$AccountService = @{
'root.branch.setting1'= 'Val1'
'root.branch.setting2'= 'Val2'
'root.branch.setting3'= 'Val3'
}
创建JSON对象
$json = Get-Content 'C:\Users\ramosfer\Documents\test.json' | ConvertFrom-Json
使用循环从数组中获取每个键以从 JSON 中获取值。在 Expression ($json.root.branch.setting1)
中期待这样的事情$AccountService.GetEnumerator() | % {
$json | Select-Object @{Name="Val"; Expression={$json.$_}}
}
使用这个 $json.$_ 并期待这样的结果
Val
---
Val1
Val2
Val3
解决嵌套属性的最佳方法是一次解决一个:)
一个更简单的例子,只检索一个值:
$json = '{"root":{"branch":{"setting1":"Value 1","setting2":"Value 2","setting3":"Value 3"}}}' |ConvertFrom-Json
# define the "path" to the value we want
$path = 'root.branch.setting1'
# split path into individual property names
$path = $path.Split('.')
# start from the root object and then start "walking" down the object hierarchy
$cursor = $json
foreach($property in $path)
{
# advance cursor one level deeper
$cursor = $cursor.$property
}
# this will now contain "Value 1"
$cursor
你可以把它变成一个简洁的小函数:
function Resolve-MemberChain
{
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[psobject[]]$InputObject,
[Parameter(Mandatory = $true, Position = 0)]
[string[]]$MemberPath,
[Parameter(Mandatory = $false)]
[string]$Delimiter
)
begin {
if($PSBoundParameters.ContainsKey('Delimiter')){
$MemberPath = $MemberPath.Split([string[]]@($Delimiter))
}
}
process {
foreach($obj in $InputObject){
$cursor = $obj
foreach($member in $MemberPath){
$cursor = $cursor.$member
}
$cursor
}
}
}
然后像这样使用它:
$json |Resolve-MemberChain 'root.branch.setting1' -Delimiter '.'
或者,如您的情况,在计算的 属性 表达式中,如下所示:
$AccountService.GetEnumerator()|%{
$path = $_.Key
$json |Select @{Name='Val';Expression={$_ |Resolve-MemberChain $path -Delimiter .}}
}