我正在尝试使用Invoke-WebRequest cmdLet创建以下要作为ReST请求主体传递的Json
{
"fields":{
"summary":"Execution Monday, August 27, 2018 3:24 PM",
"project":{
"id":10401
},
"issuetype":{
"id":10103
},
"customfield_10527":[
"IC-54829",
"IC-54830"
],
"customfield_10539":[
"IC-54831"
]
}
}使用以下powershell
Write-Output 'Creating Test Execution'
$dateString = Get-Date -Format f
$createTestExecutionBody = @{
fields = @{
summary = 'Execution ' + $dateString
project = @{
id = $projectId
}
issuetype = @{
id = $testExecutionIssueTypeId
}
customfield_10527 = ConvertTo-Json -InputObject @($testsArray)
customfield_10539 = ConvertTo-Json -InputObject @($testPlanKey)
}
}这两个customfield属性是集合。这就是我在检查请求的HTTP时所看到的
{
"fields": {
"summary": "Execution Monday, August 27, 2018 3:24 PM",
"customfield_10527": "[\r\n \"IC-54829\",\r\n \"IC-54830\"\r\n]",
"customfield_10539": "[\r\n \"IC-54831\"\r\n]",
"project": {
"id": "10401"
},
"issuetype": {
"id": "10103"
}
}
}如果我使用-Compress标志,换行符就消失了,但是我仍然得到用引号括起来的集合,并且内部引号被转义。这实际上是通过-Compress在HTTP体中传递的内容
{
"fields": {
"summary": "Execution Monday, August 27, 2018 4:50 PM",
"customfield_10527": "[\"IC-54829\",\"IC-54830\"]",
"customfield_10539": "[\"IC-54831\"]",
"project": {
"id": "10401"
},
"issuetype": {
"id": "10103"
}
}
}这两个自定义字段都是包含一个或多个值的集合,并在前面的步骤中构建为数组。我需要帮助将其全部转换,特别是将自定义字段转换为第一个json示例。
发布于 2018-08-28 07:12:57
我问了,但很快就回答了。我让事情变得更难了。这是可行的:
Write-Output 'Creating Test Execution'
$dateString = Get-Date -Format f
$createTestExecutionBody = @{
fields = @{
summary = 'Execution ' + $dateString
project = @{
id = $projectId
}
issuetype = @{
id = $testExecutionIssueTypeId
}
customfield_10527 = @(
$testsArray
)
customfield_10539 = @(
$testPlanKey
)
}
}我相信可能有更好的方法来做到这一点,所以我为我的快速问答道歉,但我想把它留在这里,以防知道powershell的人有正确的答案。
https://stackoverflow.com/questions/52047984
复制相似问题