You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PowerShell中如何将Swagger额外属性按键值对格式展示?

问题翻译与解决方法

问题描述

我在Swagger中使用了additionalProperties,在PowerShell中执行$Properties = $obj | Select-Object -ExpandProperty AdditionalProperties后,得到如下格式的输出:

Path:         ABC
Keys:         {KeyX, KeyY}
Values:       {ValueOfX, ValueOfY}
Count:        2
...   

请问是否有办法将其展示为以下格式:

Path:         ABC
KeyX:         ValueOfX
KeyY:         ValueOfY

解决方法

通过循环配对Keys和Values数组,同时保留Path的输出,就能实现格式转换:

方法一:索引循环(兼容性强)

# 输出Path行,保持原格式对齐
"Path:         $($Properties.Path)"

# 遍历索引,取出对应键值对并格式化输出
for ($i = 0; $i -lt $Properties.Count; $i++) {
    "{0}:         {1}" -f $Properties.Keys[$i], $Properties.Values[$i]
}

方法二:简洁写法(PowerShell 5.1及以上支持)

# 先输出Path
"Path:         $($Properties.Path)"

# 遍历Keys的同时匹配对应Value并输出
$Properties.Keys | ForEach-Object -Begin { $idx = 0 } -Process {
    "{0}:         {1}" -f $_, $Properties.Values[$idx++]
}

运行上述代码后,即可得到目标格式的输出。

内容的提问来源于stack exchange,提问作者Saniaaa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 20:58:08