问题描述
我尝试制作这样的对象:
$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。 根据文档,默认 -Depth 参数值为 2。所以,我像这样更改了代码
$bodyObject = [pscustomobject]@{
'fields' = [pscustomobject]@{
'fixVersions' = @([pscustomobject]@{
'id' = $releaseId
})
}
};
$bodyJson = $bodyObject | ConvertTo-Json -Depth 3; # HERE
Write-Output $bodyJson;
并得到正确的 JSON