Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 如何在pscustomobject中初始化pscustomobjects数组_Json_Powershell_Hashtable_Pscustomobject - Fatal编程技术网

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之后自己写了类似的答案