PowerShell调用rest api url并从中解析一个值

时间:2015-08-11 22:04:02

标签: powershell

我有一个文本文件rest_api.txt,其中包含JIRA问题网址,就像这样

http://jira.tzgs.com/rest/api/2/issue/INF-1000?fields=fixVersions
http://jira.tzgs.com/rest/api/2/issue/INF-1001?fields=fixVersions
http://jira.tzgs.com/rest/api/2/issue/PRJ-6000?fields=fixVersions

当这些网址被调用时,它返回Jason文件之类的结构。 E.g。

{"expand":"renderedFields,names,schema,transitions,operations,editmeta,changelog","id":"54970","self":"http://jira.tzgs.com/rest/api/2/issue/54970","key":"INF-1000","fields":{"fixVersions":[{"self":"http://jira.tzgs.com/rest/api/2/version/15541","id":"15541","name":"NETBOOK 7.0.0","archived":false,"released":false}]}}

其中," NETBOOK 7.0.0"是此特定JIRA问题的修复版本。

有人可以帮我一个PowerShell脚本来调用这些URL并解析这个修复版本值并将其写入这样的文件吗?

INF-1000     NETBOOK 7.0.0
INF-1001     NETBOOK 7.0.0
PRJ-6000     NETAPP 1.0.0

1 个答案:

答案 0 :(得分:0)

我无法为您编写代码以获取所有网址的信息(请参阅SO {#3;}问题政策)。但我会帮助解析,让你开始朝着正确的方向前进。这是PowerShell(3.0+版本)具有非常好的ConvertFrom-Json解析器的地方。

$String = '{"expand":"renderedFields,names,schema,transitions,operations,editmeta,changelog","id":"54970","self":"http://jira.tzgs.com/rest/api/2/issue/54970","key":"INF-1000","fields":{"fixVersions":[{"self":"htt
p://jira.tzgs.com/rest/api/2/version/15541","id":"15541","name":"NETBOOK 7.0.0","archived":false,"released":false}]}}'

$JsonObj = ConvertFrom-Json $String

然后返回您的字段,您可以访问它:

PS C:\> $JsonObj.key
INF-1000

PS C:\> $JsonObj.fields.fixVersions.name
NETBOOK 7.0.0