在Linux上用shell脚本解析json

srk*_*knc 4 shell json

可能重复:
使用sed和awk解析json

我有一个JSON字符串,如下例所示,我想使用"uptime"命令的值作为shell脚本中的变量,我该怎么办?

{"serverStatus":{"version":"1.6.0","uptime":527,"uptimeEstimate":526,"localTime":{"$ date":1286923624579},"globalLock":{"totalTime": 526604302,"lockTime":3499842,"ratio":0.006646056605895331,"currentQueue":{"total":0,"readers":0,"writer":0}},"mem":{"bits":64, "resident":150,"virtual":76114,"supported":true,"mapped":75950},"connections":{"current":1,"available":9599},"extra_info":{"note ":"字段因平台而异","heap_usage_bytes":600592,"page_faults":1838},"indexCounters" ....

gho*_*g74 6

您可以将编程语言与json模块一起使用,或者如果您需要shell +*nix工具解决方案,则可以使用awk

$ awk -F"[,:]" '{for(i=1;i<=NF;i++){if($i~/uptime\042/){print $(i+1)} } }' file
 527
Run Code Online (Sandbox Code Playgroud)