Json 如何在pscustomobject中初始化pscustomobjects数组
我试着做一个这样的物体:Json 如何在pscustomobject中初始化pscustomobjects数组,json,powershell,hashtable,pscustomobject,Json,Powershell,Hashtable,Pscustomobject,我试着做一个这样的物体: $bodyObject = [pscustomobject]@{ 'fields' = [pscustomobject]@{ 'fixVersions' = @([pscustomobject]@{ 'id' = $releaseId }) } }; $bodyJson = $bodyObject | ConvertTo-Json; Write-Output $bodyJson; 我得到了以下输出
$bodyObject = [pscustomobject]@{
'fields' = [pscustomobject]@{
'fixVersions' = @([pscustomobject]@{
'id' = $releaseId
})
}
};
$bodyJson = $bodyObject | ConvertTo-Json;
Write-Output $bodyJson;
我得到了以下输出:
{
"fields": {
"fixVersions": [
"@{id=16919}"
]
}
}
如何实现这样的有效JSON结构
{
"fields": {
"fixVersions": [
{"id": "16919"}
]
}
}
当我刚写了一个问题时,我想到了这个主意。 问题不在于创建复杂对象的方式,而在于json serilizator。 根据文档,默认深度参数值为2。所以,我像这样修改了代码
$bodyObject = [pscustomobject]@{
'fields' = [pscustomobject]@{
'fixVersions' = @([pscustomobject]@{
'id' = $releaseId
})
}
};
$bodyJson = $bodyObject | ConvertTo-Json -Depth 3; # HERE
Write-Output $bodyJson;
并且得到了一个正确的JSON
converttojson
->converttojson-Depth 3
(如果有深度嵌套的对象,可能需要将其设置为更高的数字),这是真的!谢谢,我刚刚在读了MSDN之后自己写了类似的答案