gpt4 book ai didi

json - 更正json以将父链接添加到TFS工作项

转载 作者:行者123 更新时间:2023-12-03 00:13:19 25 4
gpt4 key购买 nike

我正在尝试使用powershell和json将父链接添加到TFS工作项。我们有一个内部TFS服务器(即没有团队服务)。

我得到了我的查询的答案,所以我与TFS的连接正在工作,但是当我尝试更新时出现以下错误:

"You must pass a valid patch document in the body of the request."

我是一个json菜鸟,并从此 MSDN page获取了json骨架

这是我的json:
[
{
"op": "add",
"path": "/relations/-",
"value":
{
"rel": "System.LinkTypes.Hierarchy-Reverse",
"url": "https://tfs.myCompany.org/tfs/DefaultCollection/_apis/wit/workitems/259355",
"attributes":
{
{ "isLocked": false }
}
}
}
]

我根据发现的其他一些json示例在一些地方用方括号进行了测试,但它们没有帮助,因此我从上面的MSDN页面返回了语法。

这是我正在使用的powershell脚本。
param(
[System.String]$TaskId=288346
)

$username = "myUserInfo"
$password = "myPassword"

$securePassword = $password | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential($username, $securePassword)

$taskItemURL ="https://tfs.myCompanynet.org/tfs/DefaultCollection/_apis/wit/workitems/$TaskId"
$taskItemRequest = $taskItemUrl+'?$expand=relations'
$taskItemJson = Invoke-RestMethod -uri "$taskItemRequest" -Method get -
Credential $credential

if($taskItemJson.relations)
{
write-host "relation exists: " $taskItemJson.relations[0].url
}
else
{
write-host "relation does not exist. Creating it."
$jsonTemplate = Get-Content E:\scripts\JsonTemplate.txt # | ConvertTo-Json

$result = Invoke-RestMethod -uri $taskItemURL"?api-version=1.0" -Method patch -UseDefaultCredentials -ContentType application/json-patch+json -body $jsonTemplate
}

如您所见,我已注释掉convertTo-json,因为我收到此错误:
ConvertTo-Json:转换后的JSON字符串格式错误。
我不确定是否收到该错误,因为它已经是json。

我还测试了跳过get-content并使用-inFile参数,但这导致了上面的相同错误。
$result = Invoke-RestMethod -uri $taskItemURL"?api-version=1.0" -Method patch -Credential $credential -ContentType application/json-patch+json -InFile E:\scripts\JsonTemplate.txt

关于我的json有什么问题的任何想法吗?

谢谢!

最佳答案

啊!我好近我有一个猜测,即使the documentation看起来应该是这样,但attribrutes下的双花括号是错误的。当我删除它们时,它的效果很好。

现在我的json看起来像这样:

[
{
"op": "add",
"path": "/relations/-",
"value":
{
"rel": "System.LinkTypes.Hierarchy-Reverse",
"url": "https://tfs.myCompany.org/tfs/DefaultCollection/_apis/wit/workitems/259355",
"attributes":
{
"isLocked": false
}
}
}
]

关于json - 更正json以将父链接添加到TFS工作项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43522112/

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