Announcement

Collapse
No announcement yet.

Reading Json file

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

    Reading Json file

    I’m adventuring in reading json file in a script. Does anyone have an example script that I may use?
    Specifically this file {"Time":"2018-01-22T14:24:58.7031818-05:00","Offset":0,"Length":0,"Message":{"ID":XXXXXXX,"Type": 4,"TamperPhy":0,"TamperEnc":3,"Consumption":19523,"ChecksumV al":54357}}

    I google this forum for json, I did not see any example script.

    Thanks,
    Aldo
    Last edited by alphatech; January 24, 2018, 05:54 PM.

    #2
    Hope this helps. This is a snipit of a javascript I use to pull data from HomeSeer devices. I found part of this somewhere here.


    PHP Code:
                var xhr1 = new XMLHttpRequest();
                
    xhr1.open('GET''/JSON?request=getstatus&ref=' devReftrue); 
                
    xhr1.send(null);

                
    xhr1.onreadystatechange = function() {
                    if (
    xhr1.readyState == 4) {

                        var 
    loc JSON.parse(xhr1.responseText).Devices[0].location;
                        var 
    name JSON.parse(xhr1.responseText).Devices[0].name;
                        var 
    myStatus JSON.parse(xhr1.responseText).Devices[0].status;
                        var 
    deviceimage JSON.parse(xhr1.responseText).Devices[0].status_image;
                        var 
    myValue JSON.parse(xhr1.responseText).Devices[0].value;
                        
                        var 
    myTable document.getElementById("data_table");
                        var 
    myValueRound myValue.toFixed(1); 
    You can find more at https://www.w3schools.com/js/js_json.asp
    Don

    Comment


      #3
      Originally posted by donstephens View Post
      Hope this helps. This is a snipit of a javascript I use to pull data from HomeSeer devices. I found part of this somewhere here.


      PHP Code:
      var xhr1 = new XMLHttpRequest();
      xhr1.open('GET''/JSON?request=getstatus&ref=' devReftrue); 
      xhr1.send(null);

      xhr1.onreadystatechange = function() {
      if (
      xhr1.readyState == 4) {

      var 
      loc JSON.parse(xhr1.responseText).Devices[0].location;
      var 
      name JSON.parse(xhr1.responseText).Devices[0].name;
      var 
      myStatus JSON.parse(xhr1.responseText).Devices[0].status;
      var 
      deviceimage JSON.parse(xhr1.responseText).Devices[0].status_image;
      var 
      myValue JSON.parse(xhr1.responseText).Devices[0].value;

      var 
      myTable document.getElementById("data_table");
      var 
      myValueRound myValue.toFixed(1); 
      You can find more at https://www.w3schools.com/js/js_json.asp
      That is very helpful, thanks. I don't see in the script the file location where it would read the file, is there a place in hs that is the default for these files?

      Aldo

      Sent from my SM-G935V using Tapatalk

      Comment


        #4
        Let's see if I can't not bugger this :-)

        The second line xhrl.open .... sends the request to the server or web site.
        the return is parsed in the lines starting with 'JSON.parse(... '

        As it is looping through the JSON response, I'm assigning values to those responses.

        You really should explore the JSON section of the link I supplied. There are lot's of things to make your script go badly. Dates in JSON aren't permitted so you need to convert the string to a date, if you are going to do anything with it aside from just printing it out.

        I'm just getting my feet wet so I may do you more damage than good.

        Good luck.
        Don

        Comment


          #5
          Originally posted by donstephens View Post
          Let's see if I can't not bugger this :-)

          The second line xhrl.open .... sends the request to the server or web site.
          the return is parsed in the lines starting with 'JSON.parse(... '

          As it is looping through the JSON response, I'm assigning values to those responses.

          You really should explore the JSON section of the link I supplied. There are lot's of things to make your script go badly. Dates in JSON aren't permitted so you need to convert the string to a date, if you are going to do anything with it aside from just printing it out.

          I'm just getting my feet wet so I may do you more damage than good.

          Good luck.
          Not to worry, any help is greatly appreciated. Unfortunately, the JSON file I have is an actual file on my computer drive.

          Aldo

          Sent from my SM-G935V using Tapatalk

          Comment

          Working...
          X