如何显示JSON输出? [英] How to display JSON output?

查看:190
本文介绍了如何显示JSON输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

第一个API调用:

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

我得到了用户的ID,当我运行这个URI。

I get the user IDs when I run this URI.

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

Then I need to execute the next URI for each user ID generated from the previous URI:

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

然后输出文件打印到CSV。

Then print the output file to 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中的输出。

I am not getting the output in PowerShell.

推荐答案

修改

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

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

这篇关于如何显示JSON输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆