【问题标题】:PowerShell - print a JSON output with sorted array of objects?PowerShell - 使用排序的对象数组打印 JSON 输出?
【发布时间】:2021-12-10 04:13:21
【问题描述】:

如何打印带有排序对象数组的 JSON 输出? 我的 $result 对象必须保持原样,“好”或“坏”的顺序无关紧要, 我正在尝试按升序对按“count”属性排序的数组中的对象进行排序。

我的代码:

$result = [PSCustomObject]@{
    Good = @() 
    Bad  = @()
}

$food = [PSCustomObject]@{
    name  = "Banana"
    count = 2
}

if ($food.count -gt 3) { $result.Good += $food }
else { $result.Bad += $food }

$sortGood = $result.Good | Sort-Object count
$sortBad = $result.Bad | Sort-Object count
Write-Output ($result | ConvertTo-Json)

我的 JSON 输出是:

{
    "Good": [
                {
                    "name": "Apple"
                    "count": 10
                },
                {
                    "name": "Lime"
                    "count": 5
                },
                {
                    "name": "Peach"
                    "count": 7
                }
            ],
    "Bad": [
                {
                    "name": "Banana"
                    "count": 2
                },
                {
                    "name": "Kiwi"
                    "count": 1
                },
                {
                    "name": "Orange"
                    "count": 3
                }
            ] 
}

如何打印这样的 JSON? (水果按“count”属性升序排序)

{
    "Good": [
                {
                    "name": "Lime"
                    "count": 5
                },
                {
                    "name": "Peach"
                    "count": 7
                },
                {
                    "name": "Apple"
                    "count": 10
                },
            ],
    "Bad": [
                {
                    "name": "Kiwi"
                    "count": 1
                },
                {
                    "name": "Banana"
                    "count": 2
                },
                {
                    "name": "Orange"
                    "count": 3
                }
            ] 
}

[问题已修复]编辑解决方案:

$result.Good = $result.Good | Sort-Object count
$result.Bad  = $result.Bad | Sort-Object count
Write-Output ($result | ConvertTo-Json)

【问题讨论】:

    标签: arrays json powershell sorting pscustomobject


    【解决方案1】:

    Sort-Object 不会“对对象进行排序”。它返回对象的排序副本。所以这个

    $sortGood = $result.Good | Sort-Object count
    

    将导致$sortGood 被正确排序,$result.Good 与原来的完全一致。

    $json = @"
    {
        "Good": [
            {"name": "Apple", "count": 10},
            {"name": "Lime", "count": 5},
            {"name": "Peach", "count": 7}
        ],
        "Bad": [
            {"name": "Kiwi", "count": 1},
            {"name": "Orange", "count": 4}
        ] 
    }
    "@
    
    $data = ConvertFrom-Json $json
    
    $food = @{
        name  = "Banana"
        count = 2
    }
    
    if ($food.count -gt 3) {
        $data.Good += $food
    } else {
        $data.Bad += $food
    }
    
    $data.Good = $data.Good | Sort-Object count
    $data.Bad = $data.Bad | Sort-Object count
    
    $result = $data | ConvertTo-Json -Depth 10
    $result
    

    给予

    {
        "Good":  [
                     {
                         "name":  "Lime",
                         "count":  5
                     },
                     {
                         "name":  "Peach",
                         "count":  7
                     },
                     {
                         "name":  "Apple",
                         "count":  10
                     }
                 ],
        "Bad":  [
                    {
                        "name":  "Kiwi",
                        "count":  1
                    },
                    {
                        "count":  2,
                        "name":  "Banana"
                    },
                    {
                        "name":  "Orange",
                        "count":  4
                    }
                ]
    }
    

    请注意,我总是重新分配 $data.Good$data.Bad 的值:

    • 使用$data.Good += $food 创建一个以$food 结尾的新数组(!),然后将其分配给$data.Good(这是$data.Good = $data.Good + $food 的简写。)
    • 使用 $data.Good = $data.Good | Sort-Object count 以不同的顺序创建一个新数组 (!),然后将其分配给 $data.Good

    【讨论】:

    • 谢谢,问题已解决(见编辑),为什么要使用“-Depth 10”?
    • @Adela 让您了解-Depth 参数。默认情况下,PowerShell 仅将最多 2 级深度的嵌套数据转换为 JSON。比这更多的嵌套,你需要明确指定你想要多少层。 10 对我来说似乎是一个更合理的值。
    • @Adela 另外,不要将解决方案包含在您的问题中,而是将让您进入正确方向的答案标记为已接受。
    【解决方案2】:

    嘿,我猜你忘了在 Sort-Object 之后添加 -Property 即

    $sortGood = $result.Good | Sort-Object -Property count
    

    试试看告诉我!!!

    【讨论】:

    • 添加-Property实际上是没有必要的。 Sort-Object 会假设你的意思是 -Property 当你只是给一个名字。 Sort-Object count 很好。
    【解决方案3】:

    我会这样做:

    ConvertTo-Json @{
        Good = $result.Good | sort Count
        Bad = $result.Bad | sort Count
    }
    

    【讨论】:

      猜你喜欢
      • 2015-01-13
      • 2013-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-12
      • 2018-11-10
      • 1970-01-01
      • 2016-03-15
      相关资源
      最近更新 更多