No announcement yet.

JSON Deserialize - can you spot the problem?

  • Filter
  • Time
  • Show
Clear All
new posts

    JSON Deserialize - can you spot the problem?

    I've spent many hours with this script. It executes without error, but does NOT extract the JSON data for some reason, the 2nd WriteLog statement gives an empty string. Any help appreciated.

    My first post - couldn't figure out how to insert code as in other posts I've seen (sorry).

    Imports System.Web.Script.Serialization

    Sub Main(ByVal Parms As Object)
    Dim json As New JavaScriptSerializer

    Dim DataRaw as String = hs.DeviceStringByName("Big5 HTTP WU")
    ' data:
    ' {"observations":[{"stationID":"KNMSILVE59","obsTimeUtc":"2019-08-07T17:45:02Z","obsTimeLocal":"2019-08-07 11:45:02","neighborhood":"Silver City","softwareType":"myAcuRite","country":"US","solarRadiat ion":null,"lon":-108.2144928,"realtimeFrequency":null,"epoch":1565199902,"lat ":32.86399078,"uv":2.0,"winddir":317,"humidity":40,"qcSt atus ":-1,"imperial":{"temp":84,"heatIndex":83,"dewpt":57,"windChi ll ":84,"windSpeed":3,"windGust":6,"pressure":30.29,"precip Rate ":0.0,"precipTotal":0.0,"elev":6864}}]}
    hs.WriteLog("Parse", DataRaw)

    Dim jData As Observation = json.Deserialize(Of Observation)(DataRaw)
    hs.WriteLog("PARSE", jData.stationID)

    Catch Ex As Exception
    End Try

    End Sub

    Public Class Imperial
    Public Property temp As Integer
    Public Property heatIndex As Integer
    Public Property dewpt As Integer
    Public Property windChill As Integer
    Public Property windSpeed As Integer
    Public Property windGust As Integer
    Public Property pressure As Double
    Public Property precipRate As Double
    Public Property precipTotal As Double
    Public Property elev As Integer
    End Class

    Public Class Observation
    Public Property stationID As String
    Public Property obsTimeUtc As String
    Public Property obsTimeLocal As String
    Public Property neighborhood As String
    Public Property softwareType As String
    Public Property country As String
    Public Property solarRadiation As Object
    Public Property lon As Double
    Public Property realtimeFrequency As Object
    Public Property epoch As Integer
    Public Property lat As Double
    Public Property uv As Double
    Public Property winddir As Integer
    Public Property humidity As Integer
    Public Property qcStatus As Integer
    Public Property imperial As Imperial
    End Class

    Are the spaces in solarRadiat ion and other items just cut and paste issues or are they spaces?


      I checked - no spaces, this is probably a word wrap problem, since the data set is one long string - where it splits seems random. It doesn't appear when word wrap is not enabled (in an editor, for example).


        Your json data is a dictionary with the key observations which contains a list of Observation objects. So decoding that as an Observation won’t work. You need the container object which holds a list and then container.observations[0] would be what you expected. At least that’s how I’m reading the json data...


          Yup, that was it. Knew it was right in front of me. Thanks, bsobel.
          Have a great day!