简体   繁体   English

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

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

How can I print a JSON output with sorted array of objects?如何使用排序的对象数组打印 JSON 输出? My $result object must remain as it is, the order of "Good" or "Bad" doesn't matter, I'm trying to sort the objects in the arrays sorted by "count" property in ascending order.我的 $result 对象必须保持原样,“好”或“坏”的顺序无关紧要,我试图对按“count”属性按升序排序的数组中的对象进行排序。

My code:我的代码:

$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)

My JSON output is:我的 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
                }
            ] 
}

How can I print a JSON that looks like this?如何打印看起来像这样的 JSON? (fruits sorted by "count" property in ascending order) (水果按“计数”属性升序排序)

{
    "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
                }
            ] 
}

[Problem fixed] Edited solution: [问题已解决] 编辑的解决方案:

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

Sort-Object does not "sort the object". Sort-Object不会“对对象进行排序”。 It returns a sorted copy of the object.它返回对象的排序副本。 So this所以这

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

will result in $sortGood being sorted properly, and $result.Good being exactly as it was.将导致$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

gives

{
    "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
                }
            ]
}

Note that I'm always re-assigning the values of $data.Good and $data.Bad :请注意,我总是重新分配$data.Good$data.Bad的值:

  • Using $data.Good += $food creates a new array (!) with $food at the end, and then assigns that to $data.Good .使用$data.Good += $food创建一个以$food结尾的新数组 (!),然后将其分配给$data.Good (It's shorthand for $data.Good = $data.Good + $food .) (这是$data.Good = $data.Good + $food的简写。)
  • Using $data.Good = $data.Good | Sort-Object count使用$data.Good = $data.Good | Sort-Object count $data.Good = $data.Good | Sort-Object count creates a new array (!) in a different order, and then assigns that to $data.Good . $data.Good = $data.Good | Sort-Object count以不同的顺序创建一个新数组 (!),然后将其分配给$data.Good

Hey there I guess you forgot to add -Property after Sort-Object ie嘿,我猜你忘了在 Sort-Object 之后添加 -Property ie

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

Give it a try and let me know !!!试一试,让我知道!!!

I would do it like this:我会这样做:

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