gpt4 book ai didi

json - 格式化 Invoke-RestMethod 或 ConvertFrom-Json 返回的 [pscustomobject] 实例

转载 作者:行者123 更新时间:2023-12-04 15:52:12 26 4
gpt4 key购买 nike

我正在尝试从我从 RESTful API 接收到的 JSON 文件创建一个表。

当我打印 json 对象的属性时,我得到如下输出:

PS> 写入输出 JSON.Object

对象 1 : @{key1=property; key2=属性; key3=属性; key4=属性}
对象 2 : @{key1=property; key2=属性; key3=属性; key4=属性}
对象 3 : @{key1=property; key2=属性; key3=属性; key4=属性}
对象 4 : @{key1=property; key2=属性; key3=属性; key4=属性}

我想看到的输出是这样的:

名称 key1 key2 key3 key4
----- ---- ---- ---- ---- ----
Object1 属性 属性 属性 属性
Object2 属性 属性 属性 属性
Object3 属性 属性 属性 属性

此外,是否可以避免显示特定键及其属性?

例子:

名称 key1 key2 key4 # ← 不显示 key3
----- ---- ---- ----
Object1 属性 属性 属性
Object2 属性 属性 属性
Object3 属性 属性 属性

最佳答案

您需要将父键名添加为属性 Name到嵌套对象:

$json.Object | ForEach-Object {
foreach ($p in $_.PSObject.Properties) {
$p.Value | Select-Object @{n='Name';e={$p.Name}},*
}
}

请注意,PowerShell 将默认以列表形式呈现输出,因为您的对象具有 4 个以上的属性。管道通过 Format-List -AutoSize得到表格输出。

关于json - 格式化 Invoke-RestMethod 或 ConvertFrom-Json 返回的 [pscustomobject] 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41089151/

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