簡體   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