gpt4 book ai didi

powershell - 如何在PowerShell中将PSCustomObject转换为数组?

转载 作者:行者123 更新时间:2023-12-03 00:58:58 26 4
gpt4 key购买 nike

我下面有使用ConvertFrom-Json命令解析的JSON文件:

{
"item1": {
"class": "class1",
"price": "price1"
},
"item2": {
"class": "class2",
"price": "price2"
}
}

这将创建具有两个属性 PSCustomObjectitem1item2。它产生以下输出:
item1                         item2
----- -----
@{class=class1; price=price1} @{class=class2; price=price2}

我需要的是一个打印的数组:
name  class  price
---- ----- -----
item1 class1 price1
item2 class2 price2

请注意,我无法修改JSON文件的内容。

任何帮助都将不胜感激。

最佳答案

在我的同事的代码中找到了解决方案:

$json = Get-Content -Path "<file-path>" | ConvertFrom-Json;
$json.PSObject.Properties | ForEach-Object { [PSCustomObject] @{
name = $_.Name;
class = $_.Value.class;
price = $_.Value.price;
}}

关于powershell - 如何在PowerShell中将PSCustomObject转换为数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59398000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com