如何解析来自worldtides.info的JSON潮汐信息?

How to parse JSON tidal information from worldtides.info?

我的目标是解析 www.worldtides.info 的结果。我有一个 Raspberry Pi 2,我正在使用 Linux.

编写脚本

我有一个 API 密钥,curl 请求类似于:

curl -s "http://www.worldtides.info/api?extremes&lat=my_latitude&lon=my_longitude&key=my_api_key"

得到这样的结果:

{
  "status": 200,
  "requestLat": "my_latitude",
  "requestLon": "my_longitude",
  "extremes": [
    {
      "dt": 1459680132,
      "date": "2016-04-03T10:42+0000",
      "height": 0.7343567325036922,
      "type": "High"
    },
    {
      "dt": 1459702028,
      "date": "2016-04-03T16:47+0000",
      "height": -0.8438121322770741,
      "type": "Low"
    },
    {
      "dt": 1459724478,
      "date": "2016-04-03T23:01+0000",
      "height": 1.0419712550773803,
      "type": "High"
    },
    {
      "dt": 1459747135,
      "date": "2016-04-04T05:18+0000",
      "height": -1.1049607153344834,
      "type": "Low"
    },
    {
      "dt": 1459769354,
      "date": "2016-04-04T11:29+0000",
      "height": 1.1012796430343657,
      "type": "High"
    },
    {
      "dt": 1459791366,
      "date": "2016-04-04T17:36+0000",
      "height": -1.204313872235808,
      "type": "Low"
    },
    {
      "dt": 1459813613,
      "date": "2016-04-04T23:46+0000",
      "height": 1.3452661348073778,
      "type": "High"
    },
    {
      "dt": 1459835931,
      "date": "2016-04-05T05:58+0000",
      "height": -1.4062688322894952,
      "type": "Low"
    }
  ]
}

使用 jq,我想获取当天和明天的涨潮和退潮时间,但是当我尝试这样做时:

.result[].type

但是它给我一个错误:

jq: error (at <stdin>:0): Cannot iterate over null (null)`

我知道我可以用这个得到潮汐的类型:

.extremes[].type

结果是:

High
Low
etc...

潮汐时间用这个:

.extremes[].date

结果是:

2016-04-03T10:42+0000
2016-04-03T16:47+0000
etc...

那么,如何将结果组合在一起以获得这样的输出?

2016-04-03T10:42+0000 High
2016-04-03T16:47+0000 Low
etc...

类似于

curl -s "http://www.worldtides.info/api?extremes&lat=my_latitude&lon=my_longitude&key=my‌​_api_key"| jq -r ".extremes[] | .date + .type"

应该给你 both 每个条目的 date/time 和 High/Low 潮汐输出。


基本上,您需要使用 extremes 而不是 result。没有 no result 键,因此 jq 给你关于 null.

的信息