首页 > Web开发 > 详细

Shell 解析 JSON

时间:2021-02-06 23:58:03      阅读:43      评论:0      收藏:0      [点我收藏+]

Shell 下使用 jq 解析 JSON,十分方便。

源数据

$ curl ip.dhcp.cn/?json
{
  "IP": "134.175.159.160",
  "Address": {
    "Country": "中国",
    "Province": "广东省",
    "City": "广州市"
  },
  "ISP": "电信"
}

1. 使用 jq 解析 JSON 串

$ curl -s ip.dhcp.cn/?json | jq .IP
"134.175.159.160"
  • 获取字典长度
$ curl -s ip.dhcp.cn/?json | jq ".Address"
{
  "Country": "中国",
  "Province": "广东省",
  "City": "广州市"
}
$ curl -s ip.dhcp.cn/?json | jq ".Address | length"
3
  • 获取嵌套数据
$ curl -s ip.dhcp.cn/?json | jq .Address.City
"广州市"

2. Python 解析 JSON 串

$ curl -s ‘ip.dhcp.cn/?json‘ |   python -c "import sys, json; print json.load(sys.stdin)[‘IP‘]"
134.175.159.160

reference

Shell 解析 JSON

原文:https://www.cnblogs.com/watering/p/14382563.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!