如何在 Powershell 中转义特定 JSON 个字符

How to escape specific JSON characters in Powershell

背景

我正在使用 PowerShell 7。

昨天,我问了 一个关于帮助将一些 JSON 合并并保存的问题,效果很好。

我现在 运行 遇到另一个问题,我的新知识对转义字符没有帮助。

问题

我有一些 JSON 像这样:

{
    "toolcache": [
        {
            "name": "Python",
            "url": "https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json",
            "platform": "linux",
            "platform_version": "18.04",
            "arch": "x64",
            "versions": [
                "2.7.*",
                "3.6.*",
                "3.7.*",
                "3.8.*",
                "3.9.*",
                "3.10.*"
            ]
        }
    ],
     "powershellModules": [
        {"name": "MarkdownPS"},
        {"name": "Microsoft.Graph"},
        {"name": "Pester"},
        {"name": "PSScriptAnalyzer"}
    ]
}

并且我尝试将知识应用到其他方面 post:


### Add to JSON Toolet

# Try to escape characters as plain text using heredoc - Is this is the issue?
$ToolsetsToAdd = @"
{"name": "SqlServer"}
"@


$ToolsetFile = "toolset-2004.json"
$ToolsetObject = $(Get-Content $ToolsetFile -Raw)
$ToolSetSystemObject =$($ToolsetObject | ConvertFrom-Json)

$ToolSetSystemObject.powershellModules += $ToolsetsToAddJson

$ToolSetSystemObject | ConvertTo-Json -Depth 100 | Set-Content $ToolsetFile

当我 运行 它时,我得到:

{
    "toolcache": [
        {
            "name": "Python",
            "url": "https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json",
            "platform": "linux",
            "platform_version": "18.04",
            "arch": "x64",
            "versions": [
                "2.7.*",
                "3.6.*",
                "3.7.*",
                "3.8.*",
                "3.9.*",
                "3.10.*"
            ]
        }
    ],
    "powershellModules": [
    {
      "name": "MarkdownPS"
    },
    {
      "name": "Microsoft.Graph"
    },
    {
      "name": "Pester"
    },
    {
      "name": "PSScriptAnalyzer"
    },
    "{\"name\": \"SqlServer\"}"
  ],

我尝试过使用 `"`" 之类的转义变体,但也无法弄清楚这个。

据我所知,这是扩展的 JSON,所以我不确定这是不是一个错误,但它并没有像我预期的那样复制和转义内容。这是否可以避免是我想从更了解的人那里找到的!

我想做的事情:

理想情况下,我想要一个格式正确且解析正确的 JSON 文件,如下所示:

{
    "toolcache": [
        {
            "name": "Python",
            "url": "https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json",
            "platform": "linux",
            "platform_version": "18.04",
            "arch": "x64",
            "versions": [
                "2.7.*",
                "3.6.*",
                "3.7.*",
                "3.8.*",
                "3.9.*",
                "3.10.*"
            ]
        }
    ],
     "powershellModules": [
        {"name": "MarkdownPS"},
        {"name": "Microsoft.Graph"},
        {"name": "Pester"},
        {"name": "PSScriptAnalyzer"}
        {"name": "SqlServer"}
    ]
}

您将 $ToolSetSystemObject.powershellModules 中的数组视为 字符串数组 ,但实际上这是 对象数组

要向其添加新对象,只需执行

$ToolSetSystemObject.powershellModules += @{name = "SqlServer"}

$ToolSetSystemObject.powershellModules += [PsCustomObject]@{name = "SqlServer"}