如何在 JSON 表达式中包含参数:"Problems parsing JSON"
How to include an argument in a JSON expression: "Problems parsing JSON"
我想要一个脚本 git-repository
来创建一个 github 存储库,接受一个参数
# git-repository <my_repository_name>
但是我无法包含 $@ 变量。表达式:
curl -u mygituser https://api.github.com/user/repos -d "{"name":"$@"}"
报错
{
"message": "Problems parsing JSON",
"documentation_url": "https://developer.github.com/v3/repos/#create"
}
如何在 JSON 表达式中包含 $@ 变量?
使用jq
在bash中安全地生成JSON。在这种情况下,由于您需要 string 而不是 array,因此您应该使用 $*
而不是 $@
:
json=$(jq -nc --arg name "$*" '{"name": $name}')
curl -u mygituser https://api.github.com/user/repos -d "$json"
我想要一个脚本 git-repository
来创建一个 github 存储库,接受一个参数
# git-repository <my_repository_name>
但是我无法包含 $@ 变量。表达式:
curl -u mygituser https://api.github.com/user/repos -d "{"name":"$@"}"
报错
{
"message": "Problems parsing JSON",
"documentation_url": "https://developer.github.com/v3/repos/#create"
}
如何在 JSON 表达式中包含 $@ 变量?
使用jq
在bash中安全地生成JSON。在这种情况下,由于您需要 string 而不是 array,因此您应该使用 $*
而不是 $@
:
json=$(jq -nc --arg name "$*" '{"name": $name}')
curl -u mygituser https://api.github.com/user/repos -d "$json"