2016-04-03 56 views
2

我的目標是解析www.worldtides.info的結果。我有一個Raspberry Pi 2,我正在用Linux編寫腳本。如何解析來自worldtides.info的JSON潮汐信息?

我有一個API密鑰和捲曲的要求是一樣的東西:

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... 
+0

請將您希望的輸出樣本輸入添加到您的問題中。 – Cyrus

+0

沒有'結果'鍵,所以我不確定你想要做什麼。 –

+0

請爲您編輯的後半部分提交一個單獨的問題。 –

回答

0

東西沿着

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

行應該給你的日期/時間和高/低潮輸出爲每個條目。


基本上,則需要使用extremesresult。有沒有result密鑰,因此jq給你關於null的消息。