Announcement

Collapse
No announcement yet.

parse JSON and ScriptingReferences question

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

  • parse JSON and ScriptingReferences question

    Edit
    I made this edit so in case anyone is in the same position as me I hopes this info will help you.

    If you wan't to parse JSON and get neatly readable text you need to deserialize it.
    You can use the already built in JavaScriptSerializer or you can use Newtonsoft JSON.NET .

    For the JavaScriptSerializer you need to shutdown HS3 and edit the settings.ini file location in HomeSeer/Config/
    Edit of add this line just before the [Display] line
    ScriptingReferences=System.Web.Script.Serialization;System.W eb.Extensions.dll

    For Newtonsoft you also need to shutdown HS3 and edit the settings.ini file location in HomeSeer/Config/
    Edit of add this line just before the [Display] line
    ScriptingReferences=Newtonsoft.Json;Newtonsoft.Json.dll
    And add the Newtonsoft.Json.dll in the root directory of Homeseer. I got my Newtonsoft.Json.dll copy from /Homeseer/bin/homeseer/

    If your JSON string looks like this
    Code:
    {
        "UPTIME": "UNKNOWN",
        "VERSION ": 2.0,
        "SENDTIME": "03:33:52",
        "SENDDATE": "28- 12-2012",
        "HOSTNAME": "My-PC"
    }
    You can easily get each value
    Code:
    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
    
    Sub Main(params as object)
    
    Dim json As String
    Dim version As String
    Dim uptime As String
    Dim obj As New Object
    
    json = "{""UPTIME"": ""UNKNOWN"", ""VERSION"": 2.0, ""SENDTIME"": ""03:33:52"",  ""SENDDATE"": ""28- 12-2012"", ""HOSTNAME"": ""My-PC""}"
    
    obj = JsonConvert.DeserializeObject(json)
    uptime = obj.Item("UPTIME").ToString
    version = obj.Item("VERSION").ToString
    
    hs.WriteLog("Thinking cleaner Status", uptime)
    hs.WriteLog("Thinking cleaner Status", version)
    
    end sub
    HTML Code:
    https://forums.homeseer.com/forum/homeseer-products-services/system-software-controllers/hs3-hs3pro-software/hs3-pro-beta-releases/85649-json-deserialize-in-hs3-scripts-solved?t=172336&highlight=json

    However If your string looks like this.

    Code:
    {
    "Venue": {
        "ID": 3145,
        "Name": "Big Venue, Clapton",
        "NameWithTown": "Big Venue, Clapton, London",
        "NameWithDestination": "Big Venue, Clapton, London",
        "ListingType": "A",
        "Address": {
            "Address1": "Clapton Raod",
            "Address2": "",
            "Town": "Clapton",
            "County": "Greater London",
            "Postcode": "PO1 1ST",
            "Country": "United Kingdom",
            "Region": "Europe"
        },
        "ResponseStatus": {
            "ErrorCode": "200",
            "Message": "OK"
        }
    }
    }
    The JSON_result is wrapped in another property named "Venue"
    HTML Code:
    https://stackoverflow.com/questions/21676708/simple-working-example-of-json-net-in-vb-net
    So you need to create a class

    Code:
    Public Class Container
        Public Venue As JSON_result
    End Class
    
    Public Class JSON_result
        Public ID As Integer
        Public Name As String
        Public NameWithTown As String
        Public NameWithDestination As String
        Public ListingType As String
    End Class
    
    Dim obj = JsonConvert.DeserializeObject(Of Container)(...your_json...)

    This site let's you check you JSON string and make it more readable
    https://jsonlint.com

    This site creates all the public classes you JSON string needs
    http://jsonutils.com

    I hopes this helps someone else

    ------------------------------------------------------------------------------------------------------------
    Original post

    I've been reading a lot of topics but I still have several questions.

    I want to parse a JSON string and if I'm correct there are two ways to do it
    With Newtonsoft and the JavaScriptSerializer.

    The Newtonsoft needs the Newtonsoft.Json.dll which is located in the /HomeSeer/bin/homeseer/ directory
    The JavaScriptSerializer needs the System.Web.Extensions.dll (which you need to import yourself under /HomeSeer/bin/scripting
    If you use the System.Web.Extensions.dll you need to edit your settings.ini file and add the following line

    Code:
    ScriptingReferences=System.Web.Extensions.dll
    If the above info is incorrect please let me know

    Ok now the questions;
    When I edit the settings.ini and add the ScriptingReferences line, the line disappears after I restarted Homeseer and I don't see anything in the log.
    Is this method still used and I'm I using the syntax correct?
    In an other thread I see they use this line

    Code:
    ScriptingReferences=Newtonsoft.Json;C:"Program Files (x86)\HomeSeer HS3"\Bin\Newtonsoft.Json.dll
    Do I need the full path?
    Can I use this under Linux (/HomeSeer/bin/Newtonsoft.Json.dll)?

    If Newtonsoft is already include in Homeseer why do I need to edit the settings.ini file?

    Does anyone have a working code for the Newtonsoft method (I've read that this method is a lot faster)?

    I've ran this script

    Code:
    IMPORTS Newtonsoft.Json
    IMPORTS Newtonsoft.Json.Linq
    
    Sub Main(ByVal Parms As Object)
    
    Dim strReturn As String = "{}"
    
    Dim obj as New JObject
    
    obj = JsonConvert.DeserializeObject(strReturn)
    
    End Sub
    Then I get this error:
    Code:
      
    Error    Compiling script /etc/HomeSeer/scripts/Test/test.vb: 'JObject' is not declared. It may be inaccessible due to its protection level.
    Error    Compiling script /etc/HomeSeer/scripts/Test/test.vb: The import 'Newtonsoft.Json.Linq' could not be found.
    Error    Compiling script /etc/HomeSeer/scripts/Test/test.vb: The import 'Newtonsoft.Json' could not be found.
    Error    Compiling script /etc/HomeSeer/scripts/Test/test.vb: The import 'System.Core' could not be
    Code:
    Imports System.Web.Script.Serialization
    
    Sub Main(ByVal Parms As Object)
    
    Dim json As New JavaScriptSerialize  
    
    End Sub
    With this error:
    Code:
        
    Error    Compiling script /etc/HomeSeer/scripts/Test/test.vb: 'JavaScriptSerialize' is not declared. It may be inaccessible due to its protection level.
    Error    Compiling script /etc/HomeSeer/scripts/Test/test.vb: The import 'System.Web.Script.Serialization' could not be found.
    Error    Compiling script /etc/HomeSeer/scripts/Test/test.vb: The import 'System.Core' could not be found.

  • #2
    Originally posted by alan_smithee View Post
    I've been reading a lot of topics but I still have several questions.

    I want to parse a JSON string and if I'm correct there are two ways to do it
    With Newtonsoft and the JavaScriptSerializer.

    The Newtonsoft needs the Newtonsoft.Json.dll which is located in the /HomeSeer/bin/homeseer/ directory
    The JavaScriptSerializer needs the System.Web.Extensions.dll (which you need to import yourself under /HomeSeer/bin/scripting
    If you use the System.Web.Extensions.dll you need to edit your settings.ini file and add the following line

    Code:
    ScriptingReferences=System.Web.Extensions.dll
    If the above info is incorrect please let me know

    Ok now the questions;
    When I edit the settings.ini and add the ScriptingReferences line, the line disappears after I restarted Homeseer and I don't see anything in the log.
    Is this method still used and I'm I using the syntax correct?
    In an other thread I see they use this line

    ...
    I have "ScriptingReferences=System.Web.Script.Serialization;System. Web.Extensions.dll", but I don't know that it won't work as you have it. I have it as the last line in the settings group, just before the display group.
    You have to shut HS3 down to make any changes to the settings.ini file, AFAIK.

    Comment


    • #3
      Originally posted by joegr View Post

      I have "ScriptingReferences=System.Web.Script.Serialization;System. Web.Extensions.dll", but I don't know that it won't work as you have it. I have it as the last line in the settings group, just before the display group.
      You have to shut HS3 down to make any changes to the settings.ini file, AFAIK.

      Ah ok first shutdown HS3 and then edit the settings.ini works. The config now stays in the file. Thank you for that info.

      However I still get the same error when I run the script.

      Code:
      ScriptingReferences=System.Web.Script.Serialization;System.Web.Extensions.dll
      I placed the System.Web.Extensions.dll file in /HomeSeer/bin/scripting/ directory

      Code:
      Imports System.Web.Script.Serialization
      
      Sub Main(ByVal Parms As Object)
      
      Dim json As New JavaScriptSerialize  
      
      End Sub
      Error:
      Code:
      Error    Compiling script /etc/HomeSeer/scripts/Test/test.vb: 'JavaScriptSerialize' is not declared. It may be inaccessible due to its protection level.
      Error    Compiling script /etc/HomeSeer/scripts/Test/test.vb: The import 'System.Core' could not be found.

      Comment


      • #4
        Try changing it to
        Dim json As New JavaScriptSerializer

        Comment


        • #5
          Rather than:

          Dim json As New JavaScriptSerialize

          I think It should be:

          Dim json As New JavaScriptSerializer()
          Jon

          Comment


          • #6
            Originally posted by jon00 View Post
            Rather than:

            Dim json As New JavaScriptSerialize

            I think It should be:

            Dim json As New JavaScriptSerializer()
            Yes that's it. Thank you!

            One other question though, why do most scripts still use JavaScriptSerializer instead of the Newtonsoft? I've read that Newtonsoft is faster.

            Comment


            • #7
              JavaScriptSerializer is built-in to .NET so no external dll file is required. Newtonsoft.dll is used by HomeSeer & many plugins. Each instance must to be referenced separately outside the HomeSeer root folder and many plugins are compiled against different versions of the dll. For a script, this would have to be done via the scriptingreferences entry and all scripts would have to use that reference.

              For JSON parsing in a script, I don't think speed is an issue.
              Jon

              Comment


              • #8
                Originally posted by jon00 View Post
                JavaScriptSerializer is built-in to .NET so no external dll file is required. Newtonsoft.dll is used by HomeSeer & many plugins. Each instance must to be referenced separately outside the HomeSeer root folder and many plugins are compiled against different versions of the dll. For a script, this would have to be done via the scriptingreferences entry and all scripts would have to use that reference.

                For JSON parsing in a script, I don't think speed is an issue.
                Ok, thank you for clearing that up.

                Comment

                Working...
                X