Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在BASH文件/SHELL脚本中发送带有curl请求的JSON:JSON_PARSING_错误:意外字符(';)_Json_Bash_Shell - Fatal编程技术网

在BASH文件/SHELL脚本中发送带有curl请求的JSON:JSON_PARSING_错误:意外字符(';)

在BASH文件/SHELL脚本中发送带有curl请求的JSON:JSON_PARSING_错误:意外字符(';),json,bash,shell,Json,Bash,Shell,我看了以下问题,因为我在字符串插值JSON时遇到了问题,但仍然有问题 下面是代码:(很抱歉水平滚动) 它返回一个结构良好的JSON(字符串) 然后当我点燃卷发时: curl -H "Content-type: application/json" -H "Authorization:key=$FIREBASE_SERVER_KEY" -X POST -d "$JSON_DATA" https://fcm.googleapis.com/fcm/send 我得到以下错误:JSON\u解析\u

我看了以下问题,因为我在字符串插值JSON时遇到了问题,但仍然有问题

下面是代码:(很抱歉水平滚动)

它返回一个结构良好的JSON(字符串)

然后当我点燃卷发时:

curl -H "Content-type: application/json" -H "Authorization:key=$FIREBASE_SERVER_KEY" -X POST -d "$JSON_DATA" https://fcm.googleapis.com/fcm/send
我得到以下错误:
JSON\u解析\u错误:位置0处出现意外字符(')。

如果将${JSON_DATA}放在双引号之外,则会出现以下错误:

curl: (3) [globbing] unmatched brace in column 1
curl: (6) Could not resolve host: "random
curl: (6) Could not resolve host: test",
curl: (6) Could not resolve host: "body"
curl: (6) Could not resolve host: "here
curl: (6) Could not resolve host: is
curl: (6) Could not resolve host: big
curl: (6) Could not resolve host: body",
curl: (6) Could not resolve host: "sound"
curl: (3) [globbing] unmatched close brace/bracket in column 10
curl: (6) Could not resolve host: "to"
curl: (3) [globbing] unmatched close brace/bracket in column 24
JSON_PARSING_ERROR: Unexpected character (') at position 0.

去掉字符串周围的
\'
。它不需要,在JSON中无效

JSON_DATA='{"notification": {"title": "'"$TITLE"'", "body": "'"$BODY"'", "sound": "'"${SOUND}"'"}, "to": "'"$DEVICE_ID"'"}'

请注意,如果任何变量包含双引号、换行符或其他必须在JSON中转义的特殊字符,这将产生错误的结果。最好安装
jq
实用程序并使用它为您创建JSON。有关示例,请参见。

清除字符串周围的
\'
。它不需要,在JSON中无效

JSON_DATA='{"notification": {"title": "'"$TITLE"'", "body": "'"$BODY"'", "sound": "'"${SOUND}"'"}, "to": "'"$DEVICE_ID"'"}'

请注意,如果任何变量包含双引号、换行符或其他必须在JSON中转义的特殊字符,这将产生错误的结果。最好安装
jq
实用程序并使用它为您创建JSON。请参阅示例。

“$JSON\u DATA”
最后一行似乎不正确。你不应该使用没有引号的变量吗?你不应该在它周围有单引号。最好使用
jq
实用程序在shell中创建JSON,而不是试图通过串接字符串来构造它。@snek\u我也试过了。我更新了这个问题以显示它的样子:
“$JSON\u DATA”
在最后一行中似乎不正确。你不应该使用没有引号的变量吗?你不应该在它周围有单引号。最好使用
jq
实用程序在shell中创建JSON,而不是试图通过串接字符串来构造它。@snek\u我也试过了。我更新了这个问题以显示它的样子:谢谢。我测试了这个,它确实有效。我将使用
brew安装jq
,因为我想确保它可以扩展。谢谢。我测试了这个,它确实有效。我将使用
brew安装jq
,因为我想确保它可以扩展。
JSON_DATA='{"notification": {"title": "'"$TITLE"'", "body": "'"$BODY"'", "sound": "'"${SOUND}"'"}, "to": "'"$DEVICE_ID"'"}'