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

如何用PowerShell高效提取REST API返回JSON的name与href字段

高效提取JSON中的name与对应canonical href节点(PowerShell)

我需要从REST API返回的JSON数据里,高效提取每个item中的name节点,以及其links数组里rel为canonical的href节点。目前已知可以通过ConvertFrom-Json转换JSON后用foreach循环处理,但想找到更优的实现方式。

JSON示例

{
  "$schema": "api:standardResponse",
  "links": [
    {
      "rel": "canonical",
      "href": "http://localhost:8501/services/v2/replicats",
      "mediaType": "application/json"
    },
    {
      "rel": "self",
      "href": "http://localhost:8501/services/v2/replicats",
      "mediaType": "application/json"
    },
    {
      "rel": "describedby",
      "href": "http://localhost:8501/services/v2/metadata-catalog/replicats",
      "mediaType": "application/schema+json"
    }
  ],
  "messages": [],
  "response": {
    "$schema": "ogg:collection",
    "items": [
      {
        "links": [
          {
            "rel": "parent",
            "href": "http://localhost:8501/services/v2/replicats",
            "mediaType": "application/json"
          },
          {
            "rel": "canonical",
            "href": "http://localhost:8501/services/v2/replicats/RNIMDA00",
            "mediaType": "application/json"
          }
        ],
        "$schema": "ogg:collectionItem",
        "name": "RNIMDA00"
      },
      {
        "links": [
          {
            "rel": "parent",
            "href": "http://localhost:8501/services/v2/replicats",
            "mediaType": "application/json"
          },
          {
            "rel": "canonical",
            "href": "http://localhost:8501/services/v2/replicats/RNIMDA01",
            "mediaType": "application/json"
          }
        ],
        "$schema": "ogg:collectionItem",
        "name": "RNIMDA01"
      },
      {
        "links": [
          {
            "rel": "parent",
            "href": "http://localhost:8501/services/v2/replicats",
            "mediaType": "application/json"
          },
          {
            "rel": "canonical",
            "href": "http://localhost:8501/services/v2/replicats/RNIMDA02",
            "mediaType": "application/json"
          }
        ],
        "$schema": "ogg:collectionItem",
        "name": "RNIMDA02"
      }
    ]
  }
}

参考伪代码(基础实现)

$users = $response | ConvertFrom-Json
foreach ($user in $users)
{
write-host "$($user.name) has the email: $($user.email)"
}

更优实现方式

方式1:使用计算属性生成结构化结果

通过Select-Object的计算属性,直接提取所需字段并生成结构化对象,代码简洁且结果可直接用于后续处理:

# 转换JSON数据
$jsonData = $response | ConvertFrom-Json

# 提取name和对应canonical的href
$extractedData = $jsonData.response.items | Select-Object `
    @{Name = 'Name'; Expression = { $_.name }},
    @{Name = 'CanonicalHref'; Expression = { ($_.links | Where-Object rel -eq 'canonical').href }}

# 输出结果
$extractedData

方式2:管道链式处理(一行式)

利用PowerShell的管道特性,直接链式处理JSON数据,无需额外变量存储中间结果:

$response | ConvertFrom-Json `
| Select-Object -ExpandProperty response `
| Select-Object -ExpandProperty items `
| ForEach-Object {
    [PSCustomObject]@{
        Name = $_.name
        Href = ($_.links | Where-Object rel -eq 'canonical').href
    }
}

方式3:高效遍历(大数据量场景)

如果返回的JSON数据量较大,这种方式减少管道嵌套,直接遍历数组,性能更优:

$jsonData = $response | ConvertFrom-Json
$result = foreach ($item in $jsonData.response.items) {
    # 快速定位canonical链接
    $canonicalLink = $item.links | Where-Object rel -eq 'canonical'
    # 生成自定义对象
    [PSCustomObject]@{
        Name = $item.name
        Href = $canonicalLink.href
    }
}
# 输出结果
$result

优势说明

  • 相比基础的foreach循环,这些实现更符合PowerShell的管道式编程风格,代码可读性更强
  • 直接生成结构化的PSCustomObject结果,方便后续导出(如Export-Csv)、筛选或其他业务处理
  • 针对不同场景(简洁性、性能)提供了可选方案,适配不同需求

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 23:01:03