在PowerShell中,以下產生正確的結果。ConvertTo-Json將陣列展平3級深
$foo = @{}
$foo.bar = @{}
$foo.bar.buzz = @("herp","derp")
ConvertTo-Json $foo
{
"bar": {
"buzz": [
"herp",
"derp"
]
}
}
但是如果我增加一個級別,那麼陣列「嗡嗡」的扁平化到一個字符串
$foo = @{}
$foo.bar = @{}
$foo.bar.buzz = @{}
$foo.bar.buzz.bazz = @("herp","derp")
ConvertTo-Json $foo
{
"bar": {
"buzz": {
"bazz": "herp derp"
}
}
}
爲什麼PowerShell的扁平化數組轉換成字符串? 此問題與以下SO問題類似,但沒有任何目標解決方案可以解決此問題。
why-does-powershell-silently-convert-a-string-array-with-one-item-to-a-string
what-determines-whether-the-powershell-pipeline-will-unroll-a-collection