Announcement

Collapse
No announcement yet.

Looking for a sample JSON parser script

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

  • donstephens
    replied
    Got it! Thanks for all the help.

    Leave a comment:


  • donstephens
    replied
    OK, I will try that.

    Thanks for taking the time to walk me through this.

    Leave a comment:


  • zwolfpack
    replied
    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.

    Leave a comment:


  • donstephens
    replied
    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. :-(

    Leave a comment:


  • donstephens
    replied
    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

    Leave a comment:


  • donstephens
    replied
    Great! Thank you.

    Leave a comment:


  • zwolfpack
    replied
    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)

    Leave a comment:


  • donstephens
    replied
    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 

    Leave a comment:


  • bartbakels
    replied
    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

    Leave a comment:


  • joegr
    replied
    Originally posted by mrhappy View Post
    I don't in reality know why it is not working, but this should work;

    ...
    I would just like to say that I found this very helpful.
    Thanks,

    Leave a comment:


  • CJVann
    replied
    Thank you - I'm anxious to give it a go. I appreciate it!

    Leave a comment:


  • beerygaz
    replied
    Excuse simplicity and bad formatting - sent from a mobile.


    Sent from my iPhone using Tapatalk

    Leave a comment:


  • beerygaz
    replied
    For each alert as PublicClassAlertData in dataObj.alerts
    HS.writelog("JSON", alert.name)
    Next



    Sent from my iPhone using Tapatalk

    Leave a comment:


  • CJVann
    replied
    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
    
    Attached Files

    Leave a comment:


  • jfla
    replied
    Hi,

    I have a script that parse Json Data from web site

    It create and put into 5 devices in HS3 the data From EDF TEMPO

    https://particulier.edf.fr/bin/edf_r...peAlerte=TEMPO
    https://particulier.edf.fr/bin/edf_r...peAlerte=TEMPO

    You must update your system.ini with this reference:
    ScriptingReferences=Newtonsoft.Json;C:\"Your REP HS3"\Bin\Newtonsoft.Json.dll
    Jean-Francois.
    Attached Files

    Leave a comment:

Working...
X