繁体   English   中英

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

[英]How to round-trip this JSON to PSObject and back in 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}"
                               ]
                 }
}

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

使用参数Depth ,其值为3或更大。 默认值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"
                                   }
                               ]
                 }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM