从previous question开始,我一直使用数组和哈希表作为变量从嵌套数据结构中获取值。例如定义$fields
并将其提供给select
运算符:
$request = 'http://musicbrainz.org/ws/2/artist/5b11f4ce-a62d-471e-81fc-a69a8278c7da?inc=aliases&fmt=json'
$output = Invoke-WebRequest $request | ConvertFrom-Json
$fields = "name",
@{Name='begin'; Expression={$_.{life-span}.begin}},
@{Name='end'; Expression={$_.{life-span}.end}}
$output | select $fields
给我:
name begin end ---- ----- --- Nirvana 1988-01 1994-04-05
但是,如果我将数组定义为导入的JSON结构(bandFields.json)
{
"fields": [
"name",
{
"Name": "begin",
"Expression": "{$_.{life-span}.begin}"
}, {
"Name": "end",
"Expression": "{$_.{life-span}.end}"
}
]
}
尝试将其导入一个数组(类似于我之前手动指定的数组):
$request = 'http://musicbrainz.org/ws/2/artist/5b11f4ce-a62d-471e-81fc-a69a8278c7da?inc=aliases&fmt=json'
$output = Invoke-WebRequest $request | ConvertFrom-Json
$runConf = Get-Content -Raw -Path bandFields.json | ConvertFrom-Json
$fields = $runConf.fields
$output | select $fields
我明白了:
select:无法将System.Management.Automation.PSObject转换为以下类型之一{System.String,System.Management.Automation.ScriptBlock}。