【发布时间】:2020-10-29 15:35:02
【问题描述】:
如何将数组写入 json 文件?
#!/bin/sh
declare -A myarray
myarray["testkey"]="testvalue"
myarray["testkey1"]="testvalue1"
myarray["testkey2"]="testvalue2"
jq -n --arg $myarray[@] > file.json
编辑:
json 文件应包含以下内容:
{
"testkey": "testvalue",
"testkey1": "testvalue1",
"testkey2": "testvalue2"
}
【问题讨论】:
-
我写了一个答案,然后偶然发现了这个,这是一个非常接近的副本:stackoverflow.com/questions/44792241/… 它在解析整数方面有一点额外,但其他方面是一样的。