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

时间:2015-09-25 04:37:40

标签: json powershell

Powershell似乎无法正确地绕过这个JSON对象:

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

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

$json | ConvertFrom-Json | ConvertTo-Json

从中产生错误的JSON:

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

请注意,它会使“范围”变量错误。有办法解决这个问题吗?

1 个答案:

答案 0 :(得分:11)

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

$json | ConvertFrom-Json | ConvertTo-Json -Depth 3

输出

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