繁体   English   中英

PowerShell - 使用排序的对象数组打印 JSON 输出?

[英]PowerShell - print a JSON output with sorted array of objects?

如何使用排序的对象数组打印 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? (水果按“计数”属性升序排序)

{
    "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)

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 = $data.Good | Sort-Object count以不同的顺序创建一个新数组 (!),然后将其分配给$data.Good

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

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

试一试,让我知道!!!

我会这样做:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM