Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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
读取Shell脚本中的JSON变量_Json_Shell - Fatal编程技术网

读取Shell脚本中的JSON变量

读取Shell脚本中的JSON变量,json,shell,Json,Shell,如何读取shell脚本中的JSON变量(注意“JSON变量”,而不是JSON文件)? 我试过这样的方法 temp={\"name\":\"Sipdy\",\"time\":\"17:09 1985\",\"place\":\"CA\"} jq '.time' $temp 也试过了 temp={"name":"Sipdy","time":"17:09 1985","place":"CA"} jq '.time' $temp 但是上述两个命令都需要一个JSON文件名来代替“$temp”。您需要提

如何读取shell脚本中的JSON变量(注意“JSON变量”,而不是JSON文件)? 我试过这样的方法

temp={\"name\":\"Sipdy\",\"time\":\"17:09 1985\",\"place\":\"CA\"}
jq '.time' $temp
也试过了

temp={"name":"Sipdy","time":"17:09 1985","place":"CA"}
jq '.time' $temp

但是上述两个命令都需要一个JSON文件名来代替“$temp”。

您需要提供JSON文本作为jq的标准输入:

$ temp='{"name":"Sipdy","time":"17:09 1985","place":"CA"}'
$ echo $temp | jq .time                                   
"17:09 1985"
$ jq .time <<< $temp
"17:09 1985"
$temp='{“name”:“Sipdy”,“time”:“17:09 1985”,“place”:“CA”}
$echo$temp| jq.时间
"17:09 1985"
$jq.时间