【问题标题】:How to parse JSON tidal information from worldtides.info?如何解析来自 worldtides.info 的 JSON 潮汐信息?
【发布时间】:2016-07-23 00:50:43
【问题描述】:

我的目标是解析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...

【问题讨论】:

  • 请将该示例输入的所需输出添加到您的问题中。
  • 没有result 键,所以我不确定你要做什么。
  • 请为您的帖子编辑后的后半部分提交一个单独的问题。

标签: json bash jq


【解决方案1】:

类似的东西

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

应该给你both每个条目的日期/时间和高潮/低潮输出。


基本上,您需要使用extremes 而不是result。没有没有 result 键,因此jq 向您提供有关null 的消息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-22
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多