8 Replies - 343 Views - Last Post: 28 April 2019 - 12:57 PM Rate Topic: -----

#1 awnweather   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 08-August 18

Getting Weather Information From the National Weather Service API

Posted 27 April 2019 - 01:30 PM

Hello,
I have a weather application that will get weather information from two different JSON feeds. They are:
https://api.weather....rvations/latest

https://api.weather....KSZ015/forecast

I have tried the following code and I have gotten stuck:

Try
 WB.Headers.Add("User-Agent",  )
 Dim ForecastData As String = WB.DownloadString($"https://api.weather.gov/stations{TextBox3.Text}/observations/latest")

  Catch ex As Exception
End Try
End Using


I have gotten stuck after the download string line. The feed is only in JSON and I can't convert it to XML. What would be the simplest way to get the items from those two feeds and display each item (temperature, humidity, detailed forecast, etc) in their own separate label?

Where the textbox3.text in the code is where the user would type in their county code to get their local information.

Any help would be greatly appreciated!

Thank You!

Is This A Good Question/Topic? 0
  • +

Replies To: Getting Weather Information From the National Weather Service API

#2 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 223
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Getting Weather Information From the National Weather Service API

Posted 27 April 2019 - 04:04 PM

If you want it in XML use JsonConvert.DeserializeXmlNode - See Info you can find the package on Nuget. You should also look at JavascriptSerializer()
Was This Post Helpful? 0
  • +
  • -

#3 awnweather   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 08-August 18

Re: Getting Weather Information From the National Weather Service API

Posted 27 April 2019 - 05:21 PM

I have tried everything from using the jsonconvert to convert it to xml and nothing happens. In fact, if I try to convert the json to xml, the label or textbox just remains blank and nothing is converted. What is the easiest ways to get those two json feeds to display in labels? I wish they had an XML version of this. It would be a lot easier.

This post has been edited by awnweather: 27 April 2019 - 05:21 PM

Was This Post Helpful? 0
  • +
  • -

#4 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 223
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Getting Weather Information From the National Weather Service API

Posted 27 April 2019 - 06:14 PM

Show me what you tried? And I will help you. Saying you tried everything without showing what you tried doesn't make it any easier for us to see what you might be doing wrong. Saying "nothing happens" doesn't really hold much weight against the majority of programmers both here and across the world who've managed to retrieve nodes by using what I recommended.

Show me what you tried for both JavascriptSerializer and JsonConvert?

This post has been edited by Sheepings: 27 April 2019 - 06:15 PM

Was This Post Helpful? 0
  • +
  • -

#5 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 223
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Getting Weather Information From the National Weather Service API

Posted 27 April 2019 - 06:14 PM

Show me what you tried? And I will help you. Saying you tried everything without showing what you tried doesn't make it any easier for us to see what you might be doing wrong. Saying "nothing happens" doesn't really hold much weight against the majority of programmers both here and across the world who've managed to retrieve nodes by using what I recommended.

Show me what you tied for both JavascriptSerializer and JsonConvert?
Was This Post Helpful? 0
  • +
  • -

#6 awnweather   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 08-August 18

Re: Getting Weather Information From the National Weather Service API

Posted 27 April 2019 - 06:28 PM

View PostSheepings, on 27 April 2019 - 07:14 PM, said:

Show me what you tried? And I will help you. Saying you tried everything without showing what you tried doesn't make it any easier for us to see what you might be doing wrong. Saying "nothing happens" doesn't really hold much weight against the majority of programmers both here and across the world who've managed to retrieve nodes by using what I recommended.

Show me what you tied for both JavascriptSerializer and JsonConvert?


Here is the code I have tried:
Using WB As New WebClient
            Try
                WB.Headers.Add("User-Agent",   )
                Dim ForecastData As String = WB.DownloadString($"https://api.weather.gov/stations/{TextBox3.Text}/observations/latest")

                RichTextBox1.Text = ForecastData

                Dim weather As WeatherData = JsonConvert.DeserializeObject(Of WeatherData)(ForecastData)

                Label5.Text = Period.detailedForecast

            Catch ex As Exception
            End Try
        End Using


The code does get the feed from the NWS properly without any issues. The code above also has a Reference to a non-shared member requires an object reference error on the code that reads Label5.Text = Period.detailedForecast and I can't seem to figure out why it is doing that. The classes used are as follows:

Public Class Context
    Public Property wx As String
    Public Property geo As String
    Public Property unit As String
    Public Property vocab As String
End Class

Public Class Geometry
End Class

Public Class Period
    Public Property number As Integer
    Public Property name As String
    Public Property detailedForecast As String
End Class

Public Class WeatherData
    Public Property context As Context
    Public Property geometry As Geometry
    Public Property updated As DateTime
    Public Property periods As Period()
End Class


Was This Post Helpful? 0
  • +
  • -

#7 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 223
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Getting Weather Information From the National Weather Service API

Posted 27 April 2019 - 06:42 PM

I'll have a look at this more tomorrow, as its late here. (02:30AM)

Read over the information I already gave you. You don't need to convert. You should use the JavascriptSerializer. Good night for now.
Was This Post Helpful? 0
  • +
  • -

#8 Sheepings   User is offline

  • Senior Programmer
  • member icon

Reputation: 223
  • View blog
  • Posts: 1,260
  • Joined: 05-December 13

Re: Getting Weather Information From the National Weather Service API

Posted 28 April 2019 - 12:48 PM

What's the value of textbox3 on line 4? And on line 10, you are not using your class or setting anything for the label to output any value because the class member property is not shared, so make it public shared. Or else create an object reference of that class to access the property value. Making it shared is a better option.
                Dim obj_ref As New Period

                Label5.Text = obj_ref.detailedForecast

Was This Post Helpful? 0
  • +
  • -

#9 awnweather   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 08-August 18

Re: Getting Weather Information From the National Weather Service API

Posted 28 April 2019 - 12:57 PM

View PostSheepings, on 28 April 2019 - 01:48 PM, said:

What's the value of textbox3 on line 4? And on line 10, you are not using your class or setting anything for the label to output any value because the class member property is not shared, so make it public shared. Or else create an object reference of that class to access the property value. Making it shared is a better option.
                Dim obj_ref As New Period

                Label5.Text = obj_ref.detailedForecast


The value of text box 3 is the county or station codes. For my area it would be KSZ015. For current conditions it would be KICT.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1