none
Weather Underground API in JSON Format

    Question

  • Hello,

    I would like to use the Weather Underground API in JSON format instead of the XML format to get weather alerts, conditions, forecasts, radar, etc. I don't have any experience using JSON to get information from an API and items that show you how to decode a JSON feed don't work with the Weather Underground API. I want to use the JSON so that way the program will load the information faster and more efficient.

    How can I achieve this?

    Thanks!

    P.S. The user will need to be able to type their zip code in when the program starts up to get the information.

    Monday, March 13, 2017 12:31 AM

All replies

  • Hi

    You may do well to have a look HERE

    You can also use the search of this site for (say) 'NewtonSoft' - there are quite a few posts that may be helpful.


    Regards Les, Livingston, Scotland

    Monday, March 13, 2017 12:55 AM
  • Hello,

    I would like to use the Weather Underground API in JSON format instead of the XML format to get weather alerts, conditions, forecasts, radar, etc. I don't have any experience using JSON to get information from an API and items that show you how to decode a JSON feed don't work with the Weather Underground API. I want to use the JSON so that way the program will load the information faster and more efficient.

    How can I achieve this?

    Thanks!

    P.S. The user will need to be able to type their zip code in when the program starts up to get the information.

    AWL,

    I'll let you work out getting the location data - WU is pretty flexible about that but this I'll just use their example as shown here:

    https://www.wunderground.com/weather/api/d/docs?d=data/conditions

    If you go to the bottom of that page and click "Show Reponse", you'll see the resultant JSON.

    As Les pointed out, NewtonSoft has some good tools to work with including the ability to deserialize JSON into classes - but therein lies the rub... figuring out the structure of those classes.

    The following is from a series of online conversions and it needs a LOT of cleaning up, but it'll get you started:

    Option Strict On Option Explicit On Option Infer Off Imports System.IO Imports System.Net Imports Newtonsoft.Json Namespace WU ' Example shown from WU: ' http://api.wunderground.com/api/Your_Key/conditions/q/CA/San_Francisco.json Public NotInheritable Class CurrentConditions Private Sub New() End Sub Public Shared Function GetCurrentConditions(ByVal wuKey As String) As RootObject Dim retVal As RootObject = Nothing If Not String.IsNullOrWhiteSpace(wuKey) Then Dim sb As New System.Text.StringBuilder("http://api.wunderground.com/api/") sb.Append(wuKey) sb.Append("/conditions/q/CA/San_Francisco.json") Dim jsonString As String = GetStringFromURL(sb.ToString) retVal = JsonConvert.DeserializeObject(Of RootObject)(jsonString) End If Return retVal End Function Private Shared Function GetStringFromURL(ByVal url As String) As String Dim retVal As String = "" Try Dim request As WebRequest = WebRequest.Create(url) Using response As HttpWebResponse = DirectCast(request.GetResponse, HttpWebResponse) Using dataStream As Stream = response.GetResponseStream Using reader As New StreamReader(dataStream) Dim responseFromServer As String = reader.ReadToEnd() retVal = responseFromServer End Using End Using End Using Catch ex As Exception retVal = "" End Try Return retVal End Function End Class Public Class Features Public Property conditions() As Integer Get Return m_conditions End Get Set(value As Integer) m_conditions = Value End Set End Property Private m_conditions As Integer End Class Public Class Response Public Property version() As String Get Return m_version End Get Set(value As String) m_version = Value End Set End Property Private m_version As String Public Property termsofService() As String Get Return m_termsofService End Get Set(value As String) m_termsofService = Value End Set End Property Private m_termsofService As String Public Property features() As Features Get Return m_features End Get Set(value As Features) m_features = Value End Set End Property Private m_features As Features End Class Public Class Image Public Property url() As String Get Return m_url End Get Set(value As String) m_url = Value End Set End Property Private m_url As String Public Property title() As String Get Return m_title End Get Set(value As String) m_title = Value End Set End Property Private m_title As String Public Property link() As String Get Return m_link End Get Set(value As String) m_link = Value End Set End Property Private m_link As String End Class Public Class DisplayLocation Public Property full() As String Get Return m_full End Get Set(value As String) m_full = Value End Set End Property Private m_full As String Public Property city() As String Get Return m_city End Get Set(value As String) m_city = Value End Set End Property Private m_city As String Public Property state() As String Get Return m_state End Get Set(value As String) m_state = Value End Set End Property Private m_state As String Public Property state_name() As String Get Return m_state_name End Get Set(value As String) m_state_name = Value End Set End Property Private m_state_name As String Public Property country() As String Get Return m_country End Get Set(value As String) m_country = Value End Set End Property Private m_country As String Public Property country_iso3166() As String Get Return m_country_iso3166 End Get Set(value As String) m_country_iso3166 = Value End Set End Property Private m_country_iso3166 As String Public Property zip() As String Get Return m_zip End Get Set(value As String) m_zip = Value End Set End Property Private m_zip As String Public Property magic() As String Get Return m_magic End Get Set(value As String) m_magic = Value End Set End Property Private m_magic As String Public Property wmo() As String Get Return m_wmo End Get Set(value As String) m_wmo = Value End Set End Property Private m_wmo As String Public Property latitude() As String Get Return m_latitude End Get Set(value As String) m_latitude = Value End Set End Property Private m_latitude As String Public Property longitude() As String Get Return m_longitude End Get Set(value As String) m_longitude = Value End Set End Property Private m_longitude As String Public Property elevation() As String Get Return m_elevation End Get Set(value As String) m_elevation = Value End Set End Property Private m_elevation As String End Class Public Class ObservationLocation Public Property full() As String Get Return m_full End Get Set(value As String) m_full = Value End Set End Property Private m_full As String Public Property city() As String Get Return m_city End Get Set(value As String) m_city = Value End Set End Property Private m_city As String Public Property state() As String Get Return m_state End Get Set(value As String) m_state = Value End Set End Property Private m_state As String Public Property country() As String Get Return m_country End Get Set(value As String) m_country = Value End Set End Property Private m_country As String Public Property country_iso3166() As String Get Return m_country_iso3166 End Get Set(value As String) m_country_iso3166 = Value End Set End Property Private m_country_iso3166 As String Public Property latitude() As String Get Return m_latitude End Get Set(value As String) m_latitude = Value End Set End Property Private m_latitude As String Public Property longitude() As String Get Return m_longitude End Get Set(value As String) m_longitude = Value End Set End Property Private m_longitude As String Public Property elevation() As String Get Return m_elevation End Get Set(value As String) m_elevation = Value End Set End Property Private m_elevation As String End Class Public Class Estimated End Class Public Class CurrentObservation Public Property image() As Image Get Return m_image End Get Set(value As Image) m_image = Value End Set End Property Private m_image As Image Public Property display_location() As DisplayLocation Get Return m_display_location End Get Set(value As DisplayLocation) m_display_location = Value End Set End Property Private m_display_location As DisplayLocation Public Property observation_location() As ObservationLocation Get Return m_observation_location End Get Set(value As ObservationLocation) m_observation_location = Value End Set End Property Private m_observation_location As ObservationLocation Public Property estimated() As Estimated Get Return m_estimated End Get Set(value As Estimated) m_estimated = Value End Set End Property Private m_estimated As Estimated Public Property station_id() As String Get Return m_station_id End Get Set(value As String) m_station_id = Value End Set End Property Private m_station_id As String Public Property observation_time() As String Get Return m_observation_time End Get Set(value As String) m_observation_time = Value End Set End Property Private m_observation_time As String Public Property observation_time_rfc822() As String Get Return m_observation_time_rfc822 End Get Set(value As String) m_observation_time_rfc822 = Value End Set End Property Private m_observation_time_rfc822 As String Public Property observation_epoch() As String Get Return m_observation_epoch End Get Set(value As String) m_observation_epoch = Value End Set End Property Private m_observation_epoch As String Public Property local_time_rfc822() As String Get Return m_local_time_rfc822 End Get Set(value As String) m_local_time_rfc822 = Value End Set End Property Private m_local_time_rfc822 As String Public Property local_epoch() As String Get Return m_local_epoch End Get Set(value As String) m_local_epoch = Value End Set End Property Private m_local_epoch As String Public Property local_tz_short() As String Get Return m_local_tz_short End Get Set(value As String) m_local_tz_short = Value End Set End Property Private m_local_tz_short As String Public Property local_tz_long() As String Get Return m_local_tz_long End Get Set(value As String) m_local_tz_long = Value End Set End Property Private m_local_tz_long As String Public Property local_tz_offset() As String Get Return m_local_tz_offset End Get Set(value As String) m_local_tz_offset = Value End Set End Property Private m_local_tz_offset As String Public Property weather() As String Get Return m_weather End Get Set(value As String) m_weather = Value End Set End Property Private m_weather As String Public Property temperature_string() As String Get Return m_temperature_string End Get Set(value As String) m_temperature_string = Value End Set End Property Private m_temperature_string As String Public Property temp_f() As Double Get Return m_temp_f End Get Set(value As Double) m_temp_f = Value End Set End Property Private m_temp_f As Double Public Property temp_c() As Double Get Return m_temp_c End Get Set(value As Double) m_temp_c = Value End Set End Property Private m_temp_c As Double Public Property relative_humidity() As String Get Return m_relative_humidity End Get Set(value As String) m_relative_humidity = Value End Set End Property Private m_relative_humidity As String Public Property wind_string() As String Get Return m_wind_string End Get Set(value As String) m_wind_string = Value End Set End Property Private m_wind_string As String Public Property wind_dir() As String Get Return m_wind_dir End Get Set(value As String) m_wind_dir = Value End Set End Property Private m_wind_dir As String Public Property wind_degrees() As Integer Get Return m_wind_degrees End Get Set(value As Integer) m_wind_degrees = Value End Set End Property Private m_wind_degrees As Integer Public Property wind_mph() As Double Get Return m_wind_mph End Get Set(value As Double) m_wind_mph = Value End Set End Property Private m_wind_mph As Double Public Property wind_gust_mph() As Integer Get Return m_wind_gust_mph End Get Set(value As Integer) m_wind_gust_mph = Value End Set End Property Private m_wind_gust_mph As Integer Public Property wind_kph() As Integer Get Return m_wind_kph End Get Set(value As Integer) m_wind_kph = Value End Set End Property Private m_wind_kph As Integer Public Property wind_gust_kph() As Integer Get Return m_wind_gust_kph End Get Set(value As Integer) m_wind_gust_kph = Value End Set End Property Private m_wind_gust_kph As Integer Public Property pressure_mb() As String Get Return m_pressure_mb End Get Set(value As String) m_pressure_mb = Value End Set End Property Private m_pressure_mb As String Public Property pressure_in() As String Get Return m_pressure_in End Get Set(value As String) m_pressure_in = Value End Set End Property Private m_pressure_in As String Public Property pressure_trend() As String Get Return m_pressure_trend End Get Set(value As String) m_pressure_trend = Value End Set End Property Private m_pressure_trend As String Public Property dewpoint_string() As String Get Return m_dewpoint_string End Get Set(value As String) m_dewpoint_string = Value End Set End Property Private m_dewpoint_string As String Public Property dewpoint_f() As Integer Get Return m_dewpoint_f End Get Set(value As Integer) m_dewpoint_f = Value End Set End Property Private m_dewpoint_f As Integer Public Property dewpoint_c() As Integer Get Return m_dewpoint_c End Get Set(value As Integer) m_dewpoint_c = Value End Set End Property Private m_dewpoint_c As Integer Public Property heat_index_string() As String Get Return m_heat_index_string End Get Set(value As String) m_heat_index_string = Value End Set End Property Private m_heat_index_string As String Public Property heat_index_f() As String Get Return m_heat_index_f End Get Set(value As String) m_heat_index_f = Value End Set End Property Private m_heat_index_f As String Public Property heat_index_c() As String Get Return m_heat_index_c End Get Set(value As String) m_heat_index_c = Value End Set End Property Private m_heat_index_c As String Public Property windchill_string() As String Get Return m_windchill_string End Get Set(value As String) m_windchill_string = Value End Set End Property Private m_windchill_string As String Public Property windchill_f() As String Get Return m_windchill_f End Get Set(value As String) m_windchill_f = Value End Set End Property Private m_windchill_f As String Public Property windchill_c() As String Get Return m_windchill_c End Get Set(value As String) m_windchill_c = Value End Set End Property Private m_windchill_c As String Public Property feelslike_string() As String Get Return m_feelslike_string End Get Set(value As String) m_feelslike_string = Value End Set End Property Private m_feelslike_string As String Public Property feelslike_f() As String Get Return m_feelslike_f End Get Set(value As String) m_feelslike_f = Value End Set End Property Private m_feelslike_f As String Public Property feelslike_c() As String Get Return m_feelslike_c End Get Set(value As String) m_feelslike_c = Value End Set End Property Private m_feelslike_c As String Public Property visibility_mi() As String Get Return m_visibility_mi End Get Set(value As String) m_visibility_mi = Value End Set End Property Private m_visibility_mi As String Public Property visibility_km() As String Get Return m_visibility_km End Get Set(value As String) m_visibility_km = Value End Set End Property Private m_visibility_km As String Public Property solarradiation() As String Get Return m_solarradiation End Get Set(value As String) m_solarradiation = Value End Set End Property Private m_solarradiation As String Public Property UV() As String Get Return m_UV End Get Set(value As String) m_UV = Value End Set End Property Private m_UV As String Public Property precip_1hr_string() As String Get Return m_precip_1hr_string End Get Set(value As String) m_precip_1hr_string = Value End Set End Property Private m_precip_1hr_string As String Public Property precip_1hr_in() As String Get Return m_precip_1hr_in End Get Set(value As String) m_precip_1hr_in = Value End Set End Property Private m_precip_1hr_in As String Public Property precip_1hr_metric() As String Get Return m_precip_1hr_metric End Get Set(value As String) m_precip_1hr_metric = Value End Set End Property Private m_precip_1hr_metric As String Public Property precip_today_string() As String Get Return m_precip_today_string End Get Set(value As String) m_precip_today_string = Value End Set End Property Private m_precip_today_string As String Public Property precip_today_in() As String Get Return m_precip_today_in End Get Set(value As String) m_precip_today_in = Value End Set End Property Private m_precip_today_in As String Public Property precip_today_metric() As String Get Return m_precip_today_metric End Get Set(value As String) m_precip_today_metric = Value End Set End Property Private m_precip_today_metric As String Public Property icon() As String Get Return m_icon End Get Set(value As String) m_icon = Value End Set End Property Private m_icon As String Public Property icon_url() As String Get Return m_icon_url End Get Set(value As String) m_icon_url = Value End Set End Property Private m_icon_url As String Public Property forecast_url() As String Get Return m_forecast_url End Get Set(value As String) m_forecast_url = Value End Set End Property Private m_forecast_url As String Public Property history_url() As String Get Return m_history_url End Get Set(value As String) m_history_url = Value End Set End Property Private m_history_url As String Public Property ob_url() As String Get Return m_ob_url End Get Set(value As String) m_ob_url = Value End Set End Property Private m_ob_url As String Public Property nowcast() As String Get Return m_nowcast End Get Set(value As String) m_nowcast = Value End Set End Property Private m_nowcast As String End Class Public Class RootObject Public Property response() As Response Get Return m_response End Get Set(value As Response) m_response = Value End Set End Property Private m_response As Response Public Property current_observation() As CurrentObservation Get Return m_current_observation End Get Set(value As CurrentObservation) m_current_observation = Value End Set End Property Private m_current_observation As CurrentObservation End Class End Namespace


    To use it, I tested it with the following in Form1:

    Option Strict On Option Explicit On Option Infer Off Public Class Form1 Private Const apiKey As String = "YOUR_KEY_HERE" Private Sub Form1_Load(sender As System.Object, _ e As System.EventArgs) _ Handles MyBase.Load Dim conditions As WU.RootObject = _ WU.CurrentConditions.GetCurrentConditions(apiKey) Stop End Sub End Class


    When I hover my mouse over the variable "conditions", I see this:

    Now expand that out one level:

    Look at the properties of the member called "current_observation" and you'll see the data:

    Like I said earlier, it needs to be cleaned up but hopefully that will get you going in the right direction. :)


    "One who has no vices also has no virtues..."


    Monday, March 13, 2017 1:19 PM
  • VS 2015 has a paste special function to paste JSON Data as a Class, here is what I got from the URL posted below at https://www.wunderground.com/weather/api/d/docs?d=data/conditions

    Public Class WUnderground
    
        Public Class Rootobject
            Public Property response As Response
            Public Property current_observation As Current_Observation
        End Class
    
        Public Class Response
            Public Property version As String
            Public Property termsofService As String
            Public Property features As Features
        End Class
    
        Public Class Features
            Public Property conditions As Integer
        End Class
    
        Public Class Current_Observation
            Public Property image As Image
            Public Property display_location As Display_Location
            Public Property observation_location As Observation_Location
            Public Property estimated As Estimated
            Public Property station_id As String
            Public Property observation_time As String
            Public Property observation_time_rfc822 As String
            Public Property observation_epoch As String
            Public Property local_time_rfc822 As String
            Public Property local_epoch As String
            Public Property local_tz_short As String
            Public Property local_tz_long As String
            Public Property local_tz_offset As String
            Public Property weather As String
            Public Property temperature_string As String
            Public Property temp_f As Single
            Public Property temp_c As Single
            Public Property relative_humidity As String
            Public Property wind_string As String
            Public Property wind_dir As String
            Public Property wind_degrees As Integer
            Public Property wind_mph As Single
            Public Property wind_gust_mph As String
            Public Property wind_kph As Single
            Public Property wind_gust_kph As String
            Public Property pressure_mb As String
            Public Property pressure_in As String
            Public Property pressure_trend As String
            Public Property dewpoint_string As String
            Public Property dewpoint_f As Integer
            Public Property dewpoint_c As Integer
            Public Property heat_index_string As String
            Public Property heat_index_f As String
            Public Property heat_index_c As String
            Public Property windchill_string As String
            Public Property windchill_f As String
            Public Property windchill_c As String
            Public Property feelslike_string As String
            Public Property feelslike_f As String
            Public Property feelslike_c As String
            Public Property visibility_mi As String
            Public Property visibility_km As String
            Public Property solarradiation As String
            Public Property UV As String
            Public Property precip_1hr_string As String
            Public Property precip_1hr_in As String
            Public Property precip_1hr_metric As String
            Public Property precip_today_string As String
            Public Property precip_today_in As String
            Public Property precip_today_metric As String
            Public Property icon As String
            Public Property icon_url As String
            Public Property forecast_url As String
            Public Property history_url As String
            Public Property ob_url As String
        End Class
    
        Public Class Image
            Public Property url As String
            Public Property title As String
            Public Property link As String
        End Class
    
        Public Class Display_Location
            Public Property full As String
            Public Property city As String
            Public Property state As String
            Public Property state_name As String
            Public Property country As String
            Public Property country_iso3166 As String
            Public Property zip As String
            Public Property latitude As String
            Public Property longitude As String
            Public Property elevation As String
        End Class
    
        Public Class Observation_Location
            Public Property full As String
            Public Property city As String
            Public Property state As String
            Public Property country As String
            Public Property country_iso3166 As String
            Public Property latitude As String
            Public Property longitude As String
            Public Property elevation As String
        End Class
    
        Public Class Estimated
        End Class
    
    End Class
    

    Tuesday, March 14, 2017 1:24 AM