首页 > 解决方案 > PowerShell - 使用已排序的对象数组打印 JSON 输出?

问题描述

如何使用已排序的对象数组打印 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)

标签: arraysjsonpowershellsortingpscustomobject

解决方案


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.Goodand的值$data.Bad

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

推荐阅读