一、安裝
百度brew install、apt install、yum install等
二、常用操作
1.格式化json
XX命令 | jq #比如cat a.json |jq
XX命令 | jq '' #比如cat a.json | jq ''
cat a.json | jq '' | less #如果 json 很長,則可以使用管道傳給 less 命令
2.取指定字段的值
直接輸入字段,使用.嵌套訪問,例如要獲取昨天的信息
curl -s http://t.weather.sojson.com/api/weather/city/101310215 | jq .data.yesterday
響應如下: { "date": "24", "high": "高溫 29℃", "low": "低溫 23℃", "ymd": "2019-10-24", "week": "星期四", "sunrise": "06:34", "sunset": "18:10", "aqi": 27, "fx": "無持續風向", "fl": "<3級", "type": "多雲", "notice": "陰晴之間,謹防紫外線侵擾" }
3.過濾指定字段
使用一個json指定所有字段,如{date, high},獲取昨天的最高溫度如下
curl -s http://t.weather.sojson.com/api/weather/city/101310215 | jq .data.yesterday|jq "{date,high}" 響應如下: { "date": "24", "high": "高溫 29℃" }
4.獲取多個字段的值
使用逗號獲取多個
curl -s http://t.weather.sojson.com/api/weather/city/101310215 | jq .data.yesterday|jq ".date, .high"
響應如下: "24" "高溫 29℃"
5.篩選數組
直接指定數組的索引即可
curl -s http://t.weather.sojson.com/api/weather/city/101310215 |jq ".data.forecast"|jq ".[0,9]" 響應如下: { "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": "03", "high": "高溫 26℃", "low": "低溫 24℃", "ymd": "2019-11-03", "week": "星期日", "sunrise": "06:38", "sunset": "18:05", "fx": "東風", "fl": "3-4級", "type": "小雨", "notice": "雨雖小,注意保暖別感冒" }