首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在解析json并将其写回时,保持元素的顺序

在解析json并将其写回时,保持元素的顺序
EN

Stack Overflow用户
提问于 2022-08-17 13:51:57
回答 1查看 73关注 0票数 0

我在读一本json文件。更新几个值并将其写回原样。有些元素最终会失去秩序。

代码语言:javascript
复制
$manifest = (gc $manifestPath -raw) | ConvertFrom-Json -AsHashtable
$manifest.name = "$($manifest.name)-sxs"
$manifest | ConvertTo-Json -depth 100 | Out-File $manifestPath -Encoding utf8NoBOM

原始档案有:

代码语言:javascript
复制
        {
            "name": "vsVersion",
            "type": "pickList",
            "label": "Visual Studio Version",
            "required": false,
            "helpMarkDown": "If the preferred version cannot be found, the latest version found will be used instead.",
            "defaultValue": "latest",
            "options": {
                "latest": "Latest",
                "17.0": "Visual Studio 2022",
                "16.0": "Visual Studio 2019",
                "15.0": "Visual Studio 2017",
                "14.0": "Visual Studio 2015",
                "12.0": "Visual Studio 2013",
                "11.0": "Visual Studio 2012"
            }
        },

已写入的文件有:

代码语言:javascript
复制
    {
      "required": false,
      "type": "pickList",
      "name": "vsVersion",
      "options": {
        "11.0": "Visual Studio 2012",
        "12.0": "Visual Studio 2013",
        "14.0": "Visual Studio 2015",
        "17.0": "Visual Studio 2022",
        "15.0": "Visual Studio 2017",
        "16.0": "Visual Studio 2019",
        "latest": "Latest"
      },
      "helpMarkDown": "If the preferred version cannot be found, the latest version found will be used instead.",
      "label": "Visual Studio Version",
      "defaultValue": "latest"
    },

是否有办法保留元素的原始顺序?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-17 15:43:21

好的,这是REALLY recently fixed in v7.3.0-preview.6

最后,我将它添加到我的GitHub操作工作流中,以使用预览版本而不是主版本:

代码语言:javascript
复制
- run: |
    Invoke-Expression "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI -preview -quiet"
    "C:\Program Files\PowerShell\7-preview" >> $env:GITHUB_PATH
  name: Upgrade to latest preview of powershell 
  # https://github.com/PowerShell/PowerShell/issues/17404#issuecomment-1188348379

这神奇地解决了我的问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73389697

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档