gpt4 book ai didi

powershell - 循环JSON对象,返回具体对象

转载 作者:行者123 更新时间:2023-12-02 16:20:44 28 4
gpt4 key购买 nike

我正在使用 Azure Devops,并且想要循环遍历拉取请求列表。我正在使用this API request检索拉取请求列表。

当我检查 URL 时,我看到:

enter image description here

这是正确的。我有 3 个开放的拉取请求。我想要做的是检查每个对象是否有名为 sourceRefName 的特定属性。

当有匹配时,我想返回完整的对象:

enter image description here

我已经尝试过:

$listOfPullRequestsURL = "https://dev.azure.com/*****/*****/_apis/git/repositories/*****/pullrequests?api-version=5.0"
$listOfPullRequests = Invoke-RestMethod -Uri $listOfPullRequestsURL -Headers @{Authorization = $pat } -Method Get
Write-Host $listOfPullRequests
Write-Host $listOfPullRequests | ConvertFrom-Json

ForEach ($object in $listOfPullRequests) {
Write-Host "### OBJECT ###"
Write-Host $object
Write-Host $object.sourceRefName
}

结果是:

enter image description here

我如何浏览每个对象?是否可以根据 1 个属性返回整个对象?

最佳答案

$listOfPullRequestsURL = "https://dev.azure.com/****/****/_apis/git/repositories/****/pullrequests?api-version=5.0"
$listOfPullRequests = Invoke-RestMethod -Uri $listOfPullRequestsURL -Headers @{Authorization = $pat } -Method Get

$listOfPullRequests.value | ForEach-Object {
if ($_.sourceRefName -eq $env:BUILD_SOURCEBRANCH) {
Write-Host $_
}
}

这显示了正确的 JSON 对象。

关于powershell - 循环JSON对象,返回具体对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56811566/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com