Problem with JSON

Hi!

I am trying to get data from a http request json response.

When I print out response.data in the console it prints out the response.
The same with resp. (var resp = JSON.parse(JSON.stringify(response.data)); )

But when I try to print out resp.dagar[0].veckodag I get “dagar is undefined”

I have tried in a lot of different ways but no luck. I used List Paths in JSON to get the JSON path.

Is there anyone that can point me in the right direction?

Thanks

plugin.Name = "Plugin";
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 1000;
plugin.DefaultSettings = {};

function onChangeRequest(device, attribute, value) {
switch (attribute) {
    case "Switch":
        device.Switch = value;
        break;
    default:
        break;
}
}

function onConnect() {
console.log("connected");
}

function onDisconnect() {
console.log("disconnected");
}

function onPoll() {
console.log("polling");

var http = new HTTPClient();
	try {
    var response = http.get("https://sholiday.faboul.se/dagar/v2.1/",{responseType : "text"});
} catch(err) {

    console.log(err.message);
}

var resp = JSON.parse(JSON.stringify(response.data));
  
   console.log(resp.dagar[0].veckodag);

}

function onSynchronizeDevices() {
var virtualSwitch = new Device();
virtualSwitch.Id = "1";
virtualSwitch.DisplayName = "Virtual Switch";
virtualSwitch.Capabilities = ["Switch"];
virtualSwitch.Attributes = [];
plugin.Devices[virtualSwitch.Id] = virtualSwitch;
}

Here, try this code:

plugin.Name = "Plugin";
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 1000;
plugin.DefaultSettings = {};

var http = new HTTPClient();

function onChangeRequest(device, attribute, value) {
    switch (attribute) {
        case "Switch":
            device.Switch = value;
            break;
        default:
            break;
    }
}

function onConnect() {
    console.log("connected");
}

function onDisconnect() {
    console.log("disconnected");
}

function onPoll() {
    console.log("polling");
    var resp = http.get("https://sholiday.faboul.se/dagar/v2.1/");
    console.log(resp.data.dagar[0].veckodag);
}

function onSynchronizeDevices() {
    var virtualSwitch = new Device();
    virtualSwitch.Id = "1";
    virtualSwitch.DisplayName = "Virtual Switch";
    virtualSwitch.Capabilities = ["Switch"];
    virtualSwitch.Attributes = [];
    plugin.Devices[virtualSwitch.Id] = virtualSwitch;
}

Thanks thats worked.

But I actually had problem with another json that I have hosted locally on my network. I just tested with this page and I got the same problem here. But now this one works. Sadly i can’t get my “real” one to work.

Since my json is quite long I have taken out a small part of it.

{
   "dagar": [
      {
         "1 Januari": [
            "Internationella pizzadagen *"
         ],
         "2 Januari": [
            "Världsdagen för introverta *"
         ],
         "3 Januari": [
            "Internationella JRR Tolkien-dagen *"
         ]
      }
   ]
}

I tested to put that part in the code as a const for testing. When I test it in on Tryit Editor v3.7 it works. But when I put roughly the same code in HR I get “No public methods with the specified arguments were found.”

plugin.Name = "Plugin";
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 1000;
plugin.DefaultSettings = {};

var http = new HTTPClient();

function onChangeRequest(device, attribute, value) {
    switch (attribute) {
        case "Switch":
            device.Switch = value;
            break;
        default:
            break;
    }
}

function onConnect() {
    console.log("connected");
}

function onDisconnect() {
    console.log("disconnected");
}

function onPoll() {
const myJSON = '{"dagar":[{"1 Januari":["Internationella pizzadagen *"],"2 Januari":["Världsdagen för introverta *"],"3 Januari":["Internationella JRR Tolkien-dagen *"]}]}';
const myObj = JSON.parse(myJSON);
console.log(myObj.dagar[0]["3 Januari"]);
}

function onSynchronizeDevices() {
    var virtualSwitch = new Device();
    virtualSwitch.Id = "1";
    virtualSwitch.DisplayName = "Virtual Switch";
    virtualSwitch.Capabilities = ["Switch"];
    virtualSwitch.Attributes = [];
    plugin.Devices[virtualSwitch.Id] = virtualSwitch;
}

Edit: If I put my code from Tryit Editor in a web brower in HR it works there as well. But not in my plugin…

The Home Remote’s console.log function does not accept arrays. You should instead write the 1st element of the array to the console. This code here works:

plugin.Name = "Plugin";
plugin.OnChangeRequest = onChangeRequest;
plugin.OnConnect = onConnect;
plugin.OnDisconnect = onDisconnect;
plugin.OnPoll = onPoll;
plugin.OnSynchronizeDevices = onSynchronizeDevices;
plugin.PollingInterval = 1000;
plugin.DefaultSettings = {};

var http = new HTTPClient();

function onChangeRequest(device, attribute, value) {
    switch (attribute) {
        case "Switch":
            device.Switch = value;
            break;
        default:
            break;
    }
}

function onConnect() {
    console.log("connected");
}

function onDisconnect() {
    console.log("disconnected");
}

function onPoll() {
    var myJSON = '{"dagar":[{"1 Januari":["Internationella pizzadagen *"],"2 Januari":["Världsdagen för introverta *"],"3 Januari":["Internationella JRR Tolkien-dagen *"]}]}';
    var myObj = JSON.parse(myJSON);
    console.log(myObj.dagar[0]["3 Januari"][0]);
}

function onSynchronizeDevices() {
    var virtualSwitch = new Device();
    virtualSwitch.Id = "1";
    virtualSwitch.DisplayName = "Virtual Switch";
    virtualSwitch.Capabilities = ["Switch"];
    virtualSwitch.Attributes = [];
    plugin.Devices[virtualSwitch.Id] = virtualSwitch;
}
1 Like

Oh, that was what was causing my headache.
I finally got it working, after hours and hours of cursing. lol
Thanks for the help!