json - 如何将此 JSON 往返传输到 PSObject 并返回到 Powershell

标签 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}"
                               ]
                 }
}

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

最佳答案

使用参数 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"
                                   }
                               ]
                 }
}

关于json - 如何将此 JSON 往返传输到 PSObject 并返回到 Powershell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32774889/

相关文章:

java - Jackson 从简单的字符串序列化 Joda DateTime

javascript - Jquery.无法获取jSOn值

android - 如何修复 "emulator: ERROR: No AVD specified. Use ' @foo' 或 '-avd foo' 以启动名为 'foo' 的虚拟设备“

excel - 在 VS Code 中运行 PowerShell 脚本时出错,但在 PowerShell ISE 中运行良好

arrays - 如何获取数组中哈希表的索引?

powershell - 从Switch/Get-Content中获取文件名/PSChildName

csv - 使用 Powershell 从 CSV 文件中删除列

php - JSON_ENCODE 将所有数组值转换为 AJAX 请求中的字符串

json - 使用 SwiftyJSON 打印以数组开头的 JSON

json - 如何重命名 pyspark 生成的 JSON?