Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


How to read JSON 'array'? What's wrong with code?


Recommended Posts

Posted

Hi All,

 

Would have put this in Power Users forum but, for some reason, I am not allowed to post a new topic there.

 

Anyway, I am basically doing (or trying to) two things 1) Read JSON object (which works no problem) values 2) Read JSON array values (which doesn't work!).

 

How does one read an array of values? I have tried like this basic below just to prove concept and read /api/users:

 

local _HC2      = Net.FHttp(_ipaddress, _ipport)
local _response = _HC2:GET("/api/users");
local _table = json.decode(_response);
local _id       = _table.id[1];
local _name = _table.name[1];
local _type   = _table.type[1];
local _email = _table.email[1];

fibaro:call(_vdnum, "setProperty", "ui.Label1.value", _id .._name ..type .._email);

 

I've tried every other iteration I can think of i.e. starting at [0] etc etc but no luck. Yet I can do this easily with 'objects' created by Fibar API.

 

Any help appreciated, been tearing my hair out and just want to work out issue. Have looked on forums but no luck in this instance.

 

KR, Dave:)

Posted

The Index is on the table, try local _id = _table[1].id;

 

_table[1] return first user, _table[2] the second etc...

  • Topic Author
  • Posted

    Cheers J-C.

     

    Funny thing is, unless I'm going made, I tried that already today and no joy! I'm doing this in a VD.

     

    I will give another go and update:)

     

    Dave

  • Topic Author
  • Posted

    Nope, didn't work:(

     

    I assume your code is correct (indeed I thought it should work when I did it earlier today) but is there some odd behaviour in a VD that I don't know about? Basically I have this code in a button which, when pressed, should output the JSON to the label. In the button I have this, from api/users and (I also tried) api/settings/info in SAME button at same time. If I comment out this and leave just the request to api/settings/info that works....but this doesn't! Very odd!

    Posted

    Try with this code, work for me

    Please login or register to see this image.

    /emoticons/default_wink.png" alt=";)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

    Please login or register to see this code.

  • Topic Author
  • Posted

    Please login or register to see this image.

    /emoticons/default_icon_eek.gif" alt=":shock:" /> 

    Please login or register to see this image.

    /emoticons/default_icon_eek.gif" alt=":shock:" />

     

    Hmm......worked straight away! Not the loop....well that was my next bit but I'd downgraded just to get this working at all!

     

    Turns out it's the ip address. I assume 11111 is the loopback port for HC2?

     

    How come api/settings/info works on my local HC2 address (lets say 192.168.1.50:80) but api/users doesn't? Yet it works on loopback!

     

    Explanation from the master?

     

    Dave

    Please login or register to see this image.

    /emoticons/default_icon_biggrin.gif" alt=":-D" />

    Posted

    Because /settings/info is available without logging in (some basic info about version etc) and for /users you need to make authentication. When using localhost address you do not need authentication, so you can access anything.

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Reply to this topic...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...