PowerShell中WIQL的JSON转义序列错误问题求助
解决Azure DevOps WIQL执行时的JSON转义错误
问题根源
错误提示明确指向JSON转义序列错误,原因是WIQL路径中的单个反斜杠\在JSON格式的请求体里属于特殊字符,未被正确转义。当你在WIQL中写'error\wrong\folder'时,JSON解析器会把\w、\f当成非法转义序列,导致解析失败。
解决方案
将WIQL中所有路径里的单个反斜杠\替换为双反斜杠\\,确保JSON能正确解析这些路径字符。
修改后的WIQL语句
select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.Tags] from WorkItems where [System.TeamProject] = @project and [System.WorkItemType] = 'User Story' and [System.AreaPath] under 'error\\wrong\\folder' and [System.CreatedDate] <= '2022-12-16T00:00:00.0000000' and ([System.IterationPath] under 'folder\\folder' or [System.IterationPath] under 'folder\\folder')
PowerShell中构建请求体的可靠方式
推荐用PSCustomObject构建请求体,让PowerShell自动处理JSON转义,避免手动拼接字符串出错:
$wiqlQuery = @" select [System.Id], [System.WorkItemType], [System.Title], [System.AssignedTo], [System.State], [System.Tags] from WorkItems where [System.TeamProject] = @project and [System.WorkItemType] = 'User Story' and [System.AreaPath] under 'error\wrong\folder' and [System.CreatedDate] <= '2022-12-16T00:00:00.0000000' and ([System.IterationPath] under 'folder\folder' or [System.IterationPath] under 'folder\folder') "@ # 自动处理转义 $body = [PSCustomObject]@{ wiql = $wiqlQuery } | ConvertTo-Json # 执行请求 $response = Invoke-RestMethod -Method Patch -Uri $testurl -Body $body -ContentType 'application/json'
补充说明
移除and [System.AreaPath] under 'error\wrong\folder'后能正常运行,是因为该行的\w触发了明确的非法转义报错;其他路径的\f未直接报错属于偶然情况,实际上所有路径的反斜杠都需要正确转义才能保证逻辑可靠。
内容的提问来源于stack exchange,提问作者user1765312
相关产品推荐
相关产品推荐

