![jq 无法索引错误](https://linux22.com/image/711518/jq%20%E6%97%A0%E6%B3%95%E7%B4%A2%E5%BC%95%E9%94%99%E8%AF%AF.png)
我正在尝试使用 jq 从该端点获取某些信息
curl -s https://bittrex.com/api/v1.1/public/getmarketsummaries/ | jq '[.]'
输出如下
[
{
"success": true,
"message": "",
"result": [
{
"MarketName": "BTC-1ST",
"High": 0.00010322,
"Low": 9.32e-05,
"Volume": 1475820.53114847,
"Last": 9.414e-05,
"BaseVolume": 145.89904728,
"TimeStamp": "2017-10-05T00:32:45.283",
"Bid": 9.415e-05,
"Ask": 9.521e-05,
"OpenBuyOrders": 614,
"OpenSellOrders": 5887,
"PrevDay": 0.00010169,
"Created": "2017-06-06T01:22:35.727"
},
{
"MarketName": "BTC-2GIVE",
"High": 1.31e-06,
"Low": 1.24e-06,
"Volume": 4356547.69360079,
"Last": 1.29e-06,
"BaseVolume": 5.59000303,
"TimeStamp": "2017-10-05T00:21:46.333",
"Bid": 1.29e-06,
"Ask": 1.31e-06,
"OpenBuyOrders": 298,
"OpenSellOrders": 2290,
"PrevDay": 1.29e-06,
"Created": "2016-05-16T06:44:15.287"
},
{
"MarketName": "BTC-ABY",
"High": 1.89e-06,
"Low": 1.62e-06,
"Volume": 31422008.3611497,
"Last": 1.68e-06,
"BaseVolume": 53.99330434,
"TimeStamp": "2017-10-05T00:25:21.307",
"Bid": 1.68e-06,
"Ask": 1.7e-06,
"OpenBuyOrders": 437,
"OpenSellOrders": 4761,
"PrevDay": 1.63e-06,
"Created": "2014-10-31T01:43:25.743"
}
当我尝试只获取MarketName
curl -s https://bittrex.com/api/v1.1/public/getmarketsummaries/ | jq '.[] | select(.MarketName=="BTC-1ST")'
我收到以下错误
jq: error (at <stdin>:0): Cannot index boolean with string "MarketName"
其他错误
curl -s https://bittrex.com/api/v1.1/public/getmarketsummaries/ | jq '.[1]'
我得到了这个
jq: error (at <stdin>:0): Cannot index object with number
有人知道获取这些的正确命令吗?
答案1
如果您想要与{}
该 marketName 匹配的所有内容,您可以这样做
$ curl -s https://bittrex.com/api/v1.1/public/getmarketsummaries/ | jq '.result[] | select(.MarketName=="BTC-1ST")'
{
"MarketName": "BTC-1ST",
"High": 0.00010322,
"Low": 8.402e-05,
"Volume": 1820113.98502106,
"Last": 8.799e-05,
"BaseVolume": 175.00575508,
"TimeStamp": "2017-10-05T03:40:26.463",
"Bid": 8.799e-05,
"Ask": 8.817e-05,
"OpenBuyOrders": 329,
"OpenSellOrders": 6316,
"PrevDay": 0.00010101,
"Created": "2017-06-06T01:22:35.727"
}
如果你想列出所有的 MarketNames,你可以这样做
$ curl -s https://bittrex.com/api/v1.1/public/getmarketsummaries/ | jq '.result[].MarketName'
"BTC-1ST"
"BTC-2GIVE"
"BTC-ABY"
....
编程语言中列表也必须从 0 开始。因此,如果我想获取 jq 中列表中的第一个项目,我可以这样做
jq '.[0]'
列表项只有一个,因此如果你使用[1]
答案2
我知道这已经是很久以前的事了,但我能够做到这一点
| jq '.[0.].result'