通过 foreach 在 Powershell 中引用 JSON 中的数据

问题描述

我正在使用 Invoke-RestMethod 访问一些 JSON 数据,我想遍历它,为每个 id 执行另一个 Invoke-RestMethod 并为每次调用使用 apiHost 值。数据的简化示例如下:

    "items": [
    {
        "id": "1234","apiHost": "thishost.com"
    },{
        "id": "5678","apiHost": "anotherhost.com"
    },

这是我目前拥有的功能。 Write-Host 行(用于解决此问题)显示每个 id 的所有 apiHost 值并解释下一行不起作用的原因。我如何通过 id 每次迭代引用相关的 apiHost 以便下一行可以工作?

function Get-ID  {$headers = @{
    'Authorization' = $auth_string
    'Org' = $organization
    }
    $response = Invoke-RestMethod -Method Get -Headers $headers -Uri $a_url
foreach($id in $response.items.id) {

    $itemheaders = @{
        'Authorization' = $auth_string
        'Dept-ID' = $id
        }

    write-host $response.items.apiHost

    $apihost = $response.items.apiHost + '/here/there'
    $outfile = 'c:\temp\' + $id + '_details.json'
 
    $response = Invoke-RestMethod -Method Get -Headers $itemheaders -Uri $apihost -outfile $outfile
    
    }
 }

This post 似乎表明我所做的是正确的,但我认为我遗漏了一些明显的东西。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...