在Bash中解析JSON

Jér*_*rca 0 unix bash shell json curl

我正在尝试获取json数组的值,但是我没有设法获取叶子的值,有人可以帮助我吗?

这是maps.googleapis.com/maps/api中的json

我想获取持续时间文本和值,这是到目前为止我一直在使用的脚本。

function jsonValue() {
KEY=$1
num=$2
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}

curl --get --include 'http://maps.googleapis.com/maps/api/directions/json?origin=59.94473,30.294254&destination=59.80612,30.308552&sensor=false&departure_time=1346197500&mode=transit&format=json'  | jsonValue duration["value"] 2
Run Code Online (Sandbox Code Playgroud)

预先感谢,
杰里米。

Ole*_*nge 5

您正在寻找jq (轻巧灵活的命令行JSON处理器)。