如何将此JSON往返到PSObject并返回Powershell [英] How to round-trip this JSON to PSObject and back in Powershell

查看:123
本文介绍了如何将此JSON往返到PSObject并返回Powershell的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Powershell似乎无法正确往返此JSON对象:

Powershell can't seem to correctly round-trip this JSON object:

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

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

Assuming $json is a string, this command:

$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}"
                               ]
                 }
}

请注意,它错误地获取了"scope"变量.有办法解决这个问题吗?

Notice it gets the "scope" variable wrong. Is there a way to fix this?

推荐答案

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

Use the parameter Depth with value 3 or larger. The default 2 is not enough, deeper data are simply converted to strings.

$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"
                                   }
                               ]
                 }
}

这篇关于如何将此JSON往返到PSObject并返回Powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆