Announcement

Collapse
No announcement yet.

Looking for a sample JSON parser script

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • #16
    Originally posted by CJVann View Post
    I realize this is comically wrong... I'm not sure how to separate out the array. Can someone point me in the right direction?

    Code follows, and I've attached a screen capture that's a little easier on the eyes (formatting).

    Code:
    PublicSub QueryAlerts(ByVal Parms AsObject)
    Dim query AsString = hs.GetURL("http://192.168.0.125", "/query/alerts", False, 80) 
    ' data String returned from Thermostat
    ' query={"alerts":[{"name":"Air Filter","active":true},{"name":"UV Lamp","active":false},{"name":"Service","active":true}]}
    Dim jss AsNewJavaScriptSerializer
    Dim dataObj AsAlertType = jss.Deserialize(OfAlertType)(query)
    
     hs.WriteLog("Name:", dataObj.alerts.name)
    hs.WriteLog("Active:", dataObj.alerts.active)
    EndSub
    
    PublicClassAlertType
    PublicProperty alerts AsList(OfAlertData)
    EndClass
    
    PublicClassAlertData
    PublicProperty name AsString
    PublicProperty active AsBoolean
    EndClass
    
    EndClass
    
    Could you post the adpated working script? I still have issues with a similar json structure.

    Thanks

    Bart
    Regards Bart
    ------------------------------------------
    Win7 64Bit on Intel NUCI7 with SSD
    HSPRO 3.
    Devices; 1370 Events; 691

    Jon00 Scripts, JowHue, HSTouch, Plugwise, Z-wave, Ultranetatmo, Ultracam, PHlocation, BLUSBUIRT, MeiHarmony, Buienradar, MEiUnifi Pushover 3P, Random, Nest HSPhone and Blueiris

    Visonic Powermax Alarm System (HS3) Interface: http://www.domoticaforum.eu/viewtopic.php?f=68&t=11129

    Comment


    • #17
      I'm trying to deserialize a JSON string and have run into a wall.

      The web page data is

      {"station_id":690,"station_name":"Deland","public_name":"Del and","latitude":29.02807,"longitude":-81.30344,"timezone":"America/New_York",
      "elevation":21.89101982116699,"is_public":true,
      "status":{"status_code":0,"status_message":"SUCCESS"},
      etc......

      I can get all the data until I get to the status part. I'm at a loss at pulling out the sub elements in 'status'

      Here is my code. For simplicity, I copied the JSON from the web page to a file.

      How do I drill down and get all the data? For what it is worth, I'm using Newtonsoft

      PHP Code:
      IMPORTS System.IO
      IMPORTS Newtonsoft
      .Json.Linq
      IMPORTS Newtonsoft
      .Json

       Sub Main
      (byVal p As String)

          
      Dim DataFile As String hs.GetAppPath "\Data\WeatherFlow\weatherflow.txt"
          
      Dim line As String

        
      .

          
      Using reader As StreamReader = New StreamReader(DataFile)
             
      ' Read one line from file
              line = reader.ReadLine
          End Using

              Dim obj As New Object
              obj = JsonConvert.DeserializeObject(line)
              Dim StationID As String = obj.Item("station_id").ToString
              Dim StationName As String = obj.Item("station_name").ToString
              Dim PublicName As String = obj.Item("public_name").ToString
              Dim Latitude As String = obj.Item("latitude").ToString
              Dim Longitude As String = obj.Item("longitude").toString
              Dim Timezone As String = obj.Item("timezone").toString
              Dim Elevation As String = obj.Item("elevation").toString
              Dim Status As String = obj.Item().("status")

                  '
      LogIt("JSON " line,1)
                  
      Logit("Station ID " StationID,1)
                  
      Logit("Station Name " StationName,1)
                  
      LogIt("Public Name " PublicName,1)
                  
      LogIt("Latitude " latitude,1)
                  
      LogIt("Longitude " longitude,1)
                  
      LogIt("Timezone " Timezone,1)
                  
      LogIt("Elevation " elevation,1)

      End Sub 
      Don

      Comment


      • #18
        Dim status As Object = obj.Item("status")
        Dim status_message As Object = status.Item("status_message")

        Logit("Status Message " & status_message.toString, 1)

        OR

        Dim status As Object = obj.Item("status")
        Dim status_message As String = status.Item("status_message").toString

        Logit("Status Message " & status_message, 1)

        Comment


        • #19
          Great! Thank you.
          Don

          Comment


          • #20
            Still fighting this, but gotten farther, thanks to above help.

            I've been successful in getting the data from 'outdoor_keys' by using the following code

            Dim outdoor_keys As Object = obj.Item("outdoor_keys")

            For i As Integer = 0 to 20
            LogIt("outdoor_keys " & outdoor_keys(i).toString,1)
            Next


            I tried to treat the object outdoor_keys as an array but it errors when I try to loop through it using UBound(outdoor_keys)
            but works with the above snippet. If it isn't an array, what structure should I use to loop through it?

            Here is a bit of it raw

            "outdoor_keys": ["timestamp", "air_temperature", "barometric_pressure", "station_pressure", "sea_level_pressure],

            Next issue I'm having is retrieving data from the next part, 'obs'. It looks like a similar array except it has curly brackets inside the square brackets. Any
            suggestions how to code this?

            "obs": [{
            "timestamp": 1560630823,"air_temperature": 30.4,"barometric_pressure": 1014.8,"station_pressure": 1014.8,"sea_level_pressure": 1017.5}]

            Thank you
            Don

            Comment


            • #21
              A step forward. The following will loop through the 'outdoor_keys' object.

              For i As Integer = 0 to (outdoor_keys.count - 1)
              LogIt("outdoor_keys " & outdoor_keys(i).toString,1)
              Next


              Still can't figure out the 'obs' object. :-(
              Don

              Comment


              • #22
                outdoor_keys is an array of scalars, obs is an array of objects. In the example, obs array has only one member.

                Following what you figured out for outdoor_keys, maybe something like

                Dim obs0 As Object = obs(0)

                Dim timestamp As Object = obs0.Item("timestamp")

                Logit("timestamp " & timestamp.toString, 1)

                You could also iterate thru the obs elements if the array can have more than one member.

                Comment


                • #23
                  OK, I will try that.

                  Thanks for taking the time to walk me through this.
                  Don

                  Comment


                  • #24
                    Got it! Thanks for all the help.
                    Don

                    Comment

                    Working...
                    X