jq类似一个awk或grep一样的神器,可以方便地在命令行操作json
这里我使用海南万宁的天气接口做演示,地址:http://t.weather.sojson.com/api/weather/city/101310215
一、安装
官网:https://stedolan.github.io/jq/download/
基本就是brew install、apt install、yum install之类的,很简单
二、常用操作
1.格式化json
直接请求,格式明显不易读
? ~ curl -s http://t.weather.sojson.com/api/weather/city/101310215
{"message":"success感谢又拍云(upyun.com)提供CDN赞助","status":200,"date":"20191025","time":"2019-10-25 11:17:41","cityInfo":{"city":"万宁市","citykey":"101310215","parent":"海南","updateTime":"09:59"},"data":{"shidu":"92%","pm25":20.0,"pm10":49.0,"quality":"优","wendu":"25","ganmao":"各类人群可自由活动","forecast":[{"date":"25","high":"高温 29℃","low":"低温 23℃","ymd":"2019-10-25","week":"星期五","sunrise":"06:35","sunset":"18:10","aqi":44,"fx":"无持续风向","fl":"<3级","type":"中雨","notice":"记得随身携带雨伞哦"},{"date":"26","high":"高温 29℃","low":"低温 23℃","ymd":"2019-10-26","week":"星期六","sunrise":"06:35","sunset":"18:09","aqi":31,"fx":"无持续风向","fl":"<3级","type":"小雨","notice":"雨虽小,注意保暖别感冒"},{"date":"27","high":"高温 29℃","low":"低温 23℃","ymd":"2019-10-27","week":"星期日","sunrise":"06:36","sunset":"18:09","aqi":28,"fx":"无持续风向","fl":"<3级","type":"小雨","notice":"雨虽小,注意保暖别感冒"},{"date":"28","high":"高温 30℃","low":"低温 22℃","ymd":"2019-10-28","week":"星期一","sunrise":"06:36","sunset":"18:08","aqi":34,"fx":"东北风","fl":"3-4级","type":"小雨","notice":"雨虽小,注意保暖别感冒"},{"date":"29","high":"高温 27℃","low":"低温 22℃","ymd":"2019-10-29","week":"星期二","sunrise":"06:36","sunset":"18:07","aqi":35,"fx":"东北风","fl":"3-4级","type":"小雨","notice":"雨虽小,注意保暖别感冒"},{"date":"30","high":"高温 27℃","low":"低温 22℃","ymd":"2019-10-30","week":"星期三","sunrise":"06:37","sunset":"18:07","aqi":32,"fx":"无持续风向","fl":"<3级","type":"中雨","notice":"记得随身携带雨伞哦"},{"date":"31","high":"高温 29℃","low":"低温 22℃","ymd":"2019-10-31","week":"星期四","sunrise":"06:37","sunset":"18:06","fx":"东北风","fl":"3-4级","type":"中雨","notice":"记得随身携带雨伞哦"},{"date":"01","high":"高温 25℃","low":"低温 24℃","ymd":"2019-11-01","week":"星期五","sunrise":"06:38","sunset":"18:06","fx":"东北风","fl":"3-4级","type":"小雨","notice":"雨虽小,注意保暖别感冒"},{"date":"02","high":"高温 25℃","low":"低温 24℃","ymd":"2019-11-02","week":"星期六","sunrise":"06:38","sunset":"18:05","fx":"东北风","fl":"4-5级","type":"小雨","notice":"雨虽小,注意保暖别感冒"},{"date":