gpt4 book ai didi

powershell - 从外部URI获取JSON对象中的值

转载 作者:行者123 更新时间:2023-12-02 23:07:45 28 4
gpt4 key购买 nike

在我的PowerShell代码中,我有:

$retrieveSourceBranchFromBuildURL = "https://dev.azure.com/$organization/$project/_apis/build/builds/$buildId" + "?api-version=5.0"

当我浏览到浏览器中的URL时,我看到:
{
_links: {
self: {
href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/Builds/46070"
},
web: {
href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_build/results?buildId=46070"
},
sourceVersionDisplayUri: {
href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/builds/46070/sources"
},
timeline: {
href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/builds/46070/Timeline"
},
badge: {
href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/status/67"
}
},
parameters: "{"system.pullRequest.pullRequestId":"5766","system.pullRequest.sourceBranch":"refs/heads/pb/31333-test-branch/name","system.pullRequest.targetBranch":"refs/heads/master","system.pullRequest.sourceCommitId":"1cf19b95a59478a8554c2c03d65dcefe203529a6","system.pullRequest.sourceRepositoryUri":"https://something@dev.azure.com/something/something%20Suite/_git/client-web","system.pullRequest.pullRequestIteration":"1"}",
}
$buildInformation变量:
$buildInformation = Invoke-RestMethod -Uri $retrieveSourceBranchFromBuildURL -Headers @{Authorization = $pat } -Method Get -ContentType 'application/json'

当我输出 $buildInformation的值时,我得到:
 @{_links=; properties=; tags=System.Object[]; validationResults=System.Object[]; plans=System.Object[]; triggerInfo=; id=46079; buildNumber=20190624.3; status=completed; result=succeeded; queueTime=2019-06-24T07:57:05.7271255Z; startTime=2019-06-24T07:57:12.8021227Z; finishTime=2019-06-24T08:12:27.003113Z; url=https://dev.azure.com/embrace/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/Builds/46079; definition=; buildNumberRevision=3; project=; uri=vstfs:///Build/Build/46079; sourceBranch=refs/pull/5740/merge; sourceVersion=735f7a813c343dbbca5e1d1b3e966e0bad1db762; priority=normal; reason=pullRequest; requestedFor=; requestedBy=; lastChangedDate=2019-06-24T08:14:41.11Z; lastChangedBy=; parameters={"system.pullRequest.pullRequestId":"5740","system.pullRequest.sourceBranch":"refs/heads/master-md/social-create-team","system.pullRequest.targetBranch":"refs/heads/master","system.pullRequest.sourceCommitId":"e744e5c35bc3fd1539d5c49daa29147f048f3276","system.pullRequest.sourceRepositoryUri":"https://embrace@dev.azure.com/embrace/Embrace%20Suite/_git/client-web","system.pullRequest.pullRequestIteration":"5"}; orchestrationPlan=; logs=; repository=; keepForever=False; retainedByRelease=True; 

当我输出 $buildInformation.parameters的值时:
Write-Host $buildInformation.parameters
{
"system.pullRequest.pullRequestId":"5740",
"system.pullRequest.sourceBranch":"refs/heads/master-md/social-create-team",
"system.pullRequest.targetBranch":"refs/heads/master",
"system.pullRequest.sourceCommitId":"e744e5c35bc3fd1539d5c49daa29147f048f3276",
"system.pullRequest.sourceRepositoryUri":"https://embrace@dev.azure.com/embrace/Embrace%20Suite/_git/client-web",
"system.pullRequest.pullRequestIteration":"5"
}

但是当我尝试读取一个值时:
Write-Host $buildInformation.parameters.system.pullRequest.sourceBranch
输出为空。

那么,如何检索 parameters.system.pullRequest.sourceBranch值?

最佳答案

要使用名称中带有“特殊字符”的属性访问,请将参数名称用引号引起来,如下所示:

$buildInformation.parameters.'system.pullRequest.sourceBranch'

注意:这里的技巧是 parameters的值在双引号内。因此,您可能无法以通常的方式访问 parameters下的属性。要解决此问题,您可以直接在 parameters上进行转换:

$convertedParams = $buildInformation.parameters | ConvertFrom-Json

# Access desired property
$convertedParams.'system.pullRequest.sourceBranch'

当您尝试访问属性时

$buildInformation.parameters.system.pullRequest.sourceBranch

JSON的结构应如下所示:

PS> $buildInformation = '{
"parameters": {
"system": {
"pullRequest":{
"sourceBranch": "refs/heads/master-md/social-create-team"
}
}
}
}' | ConvertFrom-Json

# Checking output
PS> $buildInformation.parameters.system.pullRequest.sourceBranch
refs/heads/master-md/social-create-team

关于powershell - 从外部URI获取JSON对象中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56732869/

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