How to reduce the number of data fields returned by Dark Sky (weather API)?

丶灬走出姿态 提交于 2019-12-11 19:46:26

问题


I am using the following command to get weather forecast data from Dark Sky...

Invoke-RestMethod -Method Get -Uri "https://api.darksky.net/forecast/[myAPIkey]/25.1234,-95.1234,2018-12-25T12:00:00?exclude=currently,minutely,hourly,flags&units=us" -Headers $headers | Select-Object -ExpandProperty daily"

This is the weather information that is returned...

{"latitude":25.1234,"longitude":-95.1234,"timezone":"Etc/GMT+6","daily":{"data":[{"time":1545717600,"summary":"Mostly cloudy starting in the afternoon.","icon":"partly-cloudy-night","sunriseTime":1545743078,"sunsetTime":1545781154,"moonPhase":0.61,"precipIntensity":0.0009,"precipIntensityMax":0.0023,"precipIntensityMaxTime":1545728400,"precipProbability":0.16,"precipType":"rain","temperatureHigh":73.63,"temperatureHighTime":1545764400,"temperatureLow":72.73,"temperatureLowTime":1545800400,"apparentTemperatureHigh":74.26,"apparentTemperatureHighTime":1545764400,"apparentTemperatureLow":73.69,"apparentTemperatureLowTime":1545796800,"dewPoint":64.96,"humidity":0.77,"pressure":1017.44,"windSpeed":16.35,"windGust":25.84,"windGustTime":1545800400,"windBearing":129,"cloudCover":0.37,"uvIndex":6,"uvIndexTime":1545760800,"visibility":10,"ozone":225.63,"temperatureMin":71.09,"temperatureMinTime":1545717600,"temperatureMax":73.63,"temperatureMaxTime":1545764400,"apparentTemperatureMin":71.32,"apparentTemperatureMinTime":1545717600,"apparentTemperatureMax":74.26,"apparentTemperatureMaxTime":1545764400}]},"offset":-6}

Is there any way to filter results when making the call? Trying to reduce the amount of data stored locally. I am only looking for these few data points:

temperatureHigh
temperatureLow
precipType
precipProbability
humidity
windSpeed

回答1:


You can just do this directly...

(Invoke-RestMethod -Method Get -Uri "https://api.darksky.net/forecast/[myApiKey]/25.1234,-95.1234,2018-12-25T12:00:00?exclude=currently,minutely,hourly,flags&units=us" -Headers $headers | 
Select-Object -ExpandProperty daily).data | 
Select temperatureHigh,temperatureLow,precipType,precipProbability,humidity,windSpeed

# Results

temperatureHigh   : 73.63
temperatureLow    : 72.73
precipType        : rain
precipProbability : 0.16
humidity          : 0.77
windSpeed         : 16.35



回答2:


Use ConvertFrom-Json and Select-Object

$Json = Invoke-RestMethod -Method Get -Uri "https://api.darksky.net/forecast/[myAPIkey]/25.1234,-95.1234,2018-12-25T12:00:00?exclude=currently,minutely,hourly,flags&units=us" -Headers $headers | 
    Select-Object -ExpandProperty daily | ConvertFrom-Json

$Json.daily.data | Select-Object temperatureHigh,temperatureLow,precipType,precipProbability,humidity,windSpeed

Sample output:

temperatureHigh   : 73,63
temperatureLow    : 72,73
precipType        : rain
precipProbability : 0,16
humidity          : 0,77
windSpeed         : 16,35


来源:https://stackoverflow.com/questions/53935764/how-to-reduce-the-number-of-data-fields-returned-by-dark-sky-weather-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!