如何将此JSON往返于PSObject并返回Powershell

And*_*ott 11 powershell json

Powershell似乎无法正确地往返这个JSON对象:

{
    "settings": {
        "minimumApproverCount": 2,
        "creatorVoteCounts": false,
        "scope": [
            {
                "refName": "refs/heads/d14rel",
                "matchKind": "Exact",
                "repositoryId": "a290117c-5a8a-40f7-bc2c-f14dbe3acf6d"
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

假设$json是一个字符串,这个命令:

$json | ConvertFrom-Json | ConvertTo-Json
Run Code Online (Sandbox Code Playgroud)

从中产生错误的JSON:

{
    "settings":  {
                     "minimumApproverCount":  2,
                     "creatorVoteCounts":  false,
                     "scope":  [
                                   "@{refName=refs/heads/d14rel; matchKind=Exact; repositoryId=a290117c-5a8a-40f7-bc2c-f14db
e3acf6d}"
                               ]
                 }
}
Run Code Online (Sandbox Code Playgroud)

请注意,它会使"范围"变量错误.有没有办法来解决这个问题?

Rom*_*min 12

使用Depth值为3或更大的参数.默认值2是不够的,更深层次的数据只是转换为字符串.

$json | ConvertFrom-Json | ConvertTo-Json -Depth 3
Run Code Online (Sandbox Code Playgroud)

产量

{
    "settings":  {
                     "minimumApproverCount":  2,
                     "creatorVoteCounts":  false,
                     "scope":  [
                                   {
                                       "refName":  "refs/heads/d14rel",
                                       "matchKind":  "Exact",
                                       "repositoryId":  "a290117c-5a8a-40f7-bc2c-f14dbe3acf6d"
                                   }
                               ]
                 }
}
Run Code Online (Sandbox Code Playgroud)