如何显示JSON输出?

时间:2016-02-08 23:21:23

标签: json api rest powershell jira-rest-api

要调用的第一个API:

https://proto123.hipchat.com/v2/user?auth_token=2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

我在运行此URI时获取用户ID。

然后我需要为从前一个URI生成的每个用户ID执行下一个URI:

https://proto123.hipchat.com/v2/user/id?auth_token=2xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

然后将输出文件打印为CSV。

我该怎么做?

请检查此脚本:

$uri = "https://proto123.hipchat.com/v2/user?auth_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
try {
  $webResponse = Invoke-RestMethod -uri $uri -ContentType "application/json"
} catch {
  $webResponse = $_.Exception.Response
}

if ($webResponse) {
  Write-Debug "Output result"

  $webResponseIDList = @() # Defining array for collecting the ID's
  foreach ($webResponseID in $webResponse) {
    $webResponseIDList += [PSCustomObject] @{
      id = $webResponseID.id
    } # End of ID's Array
    $id = $webResponseID.id

    Write-Host "User ID is : $id" + $webResponseIDList `r`n
    $uri = "https://proto123.hipchat.com/v2/user/$id?auth_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 
    $webResponseID = Invoke-RestMethod -uri $uri -ContentType "application/json"

    Write-Host "User ID are :" + $webResponseID.id `r`n
    Write-Output "User ID are :" + $webResponseID.id `r`n
  }
} else {
  Write-Debug "No results were returned"
}

我没有在PowerShell中获得输出。

1 个答案:

答案 0 :(得分:1)

更改

foreach ($webResponseID in $webResponse) {
  ...
}

foreach ($webResponseID in $webResponse.items) {
  ...
}