Announcement

Collapse
No announcement yet.

Parsing these JSON responses using Newtonsoft

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

  • Parsing these JSON responses using Newtonsoft

    I'm having trouble deserializing these two json responses:

    {"temperature":{"poolTemp":47,"spaTemp":47,"airTemp":57,"sol arTemp":0,"freeze":0}}

    {"pump":{"1":{"pump":1,"name":"Pump 1","type":"VS","time":"2:43 PM","run":10,"mode":0,"drivestate":0,"watts":1458,"rpm":2850 ,"gpm":0,"ppc":0,"err":0,"timer":1,"duration":"durationnotse t"}}}

    Does anyone have an example of how to do this in script? I'm trying to get "airTemp" from the first one and "rpm" from the second one in vb.net

    Your help would be greatly appreciated. Thanks much, Chris

  • #2
    When the key is known (e.g. air temp) then I will use regular expression or function instr to locate the data of interest. Usually implemented as a function where the haystack and needle are input parameters. Only when I do know know the keyword will I deserialize, but I have not used newtonsoft.

    Comment


    • #3
      Code:
      Imports Newtonsoft.Json
      Sub Main(p As String)
      
          Dim obj As Object = JsonConvert.DeserializeObject(p)
      
          Dim temperature As Object = obj.Item("temperature")
      
          hs.WriteLog("temperature", temperature.ToString)
      
          Dim airTemp As String = temperature.Item("airTemp").ToString
      
          hs.WriteLog("airTemp", airTemp)
      
      End Sub
      Pass the 1st JSON string to the script in the parameters field.

      Comment


      • #4
        Thank you both for your responses. I used the newtonsoft method posted by zwolfpack. In case anyone is wondering, this was my result for both responses.

        Code:
        IMPORTS Newtonsoft.Json.Linq
        IMPORTS Newtonsoft.Json
        Sub Main(p As String)
        Dim strReturn As String = hs.GetURL(“192.168.1.50”,”/temperature/”, FALSE, 3000)
        Dim obj As Object = JsonConvert.DeserializeObject(strReturn)
        Dim temperature As Object = obj.Item("temperature")
        hs.WriteLog("temperature", temperature.ToString)
        Dim airTemp As String = temperature.Item("airTemp").ToString
        hs.WriteLog("airTemp", airTemp)
        End Sub
        Code:
        IMPORTS Newtonsoft.Json.Linq
        IMPORTS Newtonsoft.Json
        Sub Main(p As String)
        Dim strReturn As String = hs.GetURL(“192.168.1.50”,”/pump/”, FALSE, 3000)
        Dim obj As Object = JsonConvert.DeserializeObject(strReturn)
        Dim pump As Object = obj.Item("pump")
        hs.WriteLog("pump", pump.ToString)
        Dim one as Object = pump.Item("1").ToString
        hs.WriteLog("one", one.ToString)
        Dim obj2 As Object = JsonConvert.DeserializeObject(one.ToString)
        Dim rpm As String = obj2.Item("rpm").ToString
        hs.WriteLog("rpm", rpm)
        End Sub

        Comment


        • #5
          I have a script JavaScript that does a lot of JSON, and wanted to see how I would do it from Visual Basic (without having to create a lot of classes ahead of time which is what I've seen in other implementations). Thanks for the example! Unfortunately, HomeSeer, when running in Linux, doesn't support JavaScript as a scripting language, so I've been migrating my JavaScript scripts to Visual Basic to give me the flexibility to move to Ubuntu in the future (I actually migrated a while back, only to discover that HomeSeer in Linux doesn't run Javascript scripts :-( ).

          Comment

          Working...
          X