我正在尝试通过 Linux CLI 为 Jenkins 传递 JSON 参数来构建作业。但我无法传递 JSON 中的参数。我已使用 -g 关闭通配符。但问题仍然存在。任何帮助都将不胜感激。
curl -X POST -u username:password -g JENKINS_URL/job/Bulk_Job/buildWithParameters?serviceBranchJson="{"key1":"value1","key2":"value2"}"}
错误信息
Processing provided DSL script
{key1:value1,key2:value2}}
groovy.json.JsonException: expecting '}' or ',' but got current char 'k' with an int value of 107
The current character read is 'k' with an int value of 107
expecting '}' or ',' but got current char 'k' with an int value of 107
line number 1
index number 1
{key1:value1,key2:value2}}
代码片段
import groovy.json.JsonSlurper
def serviceBranchJson = serviceBranchJson
println(serviceBranchJson)
Map servicesMap = new JsonSlurper().parseText(serviceBranchJson)
答案1
第一:左右花括号的数量不匹配(左一个,右两个)。第二:当你在命令行上用引号输入参数时,shell 会将其理解为“不要触碰里面的内容”,但会将其传递给没有引号的进程。查看此示例的结果:
echo "{"key1":"value1","key2":"value2"}"
因此,我尝试将整个表达式括在单引号中或转义单引号,如下所示:
echo '{"key1":"value1","key2":"value2"}'
或这个:
echo "{\"key1\":\"value1\",\"key2\":\"value2\"}"