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


  • 0

http GET array question


AutoFrank

Question

Hi

Trying to learn a little more about http requests and api. I'm looking for a little help here and sorry if it's a rookie question....

 

I have the following code (thanks peterb)

Please login or register to see this code.

it creates the following output

Please login or register to see this code.

I am trying to display the zone data in the array such as id, type

 

I thought it should be something like 

Please login or register to see this code.

but I don't know enough about how to extract the various parts from an array

 

 

I tried to find a good post on the forum but couldn't but if there is one I don't mind picking my way through it.

 

any help (including an explanation) would be really appreciated...

 

Thanks

-F

 

 

 

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0

Let me first "prettify" that chunk of JSON:
 

Please login or register to see this code.

 
 
You can say that each level is separated by a dot, if the key is a string. In this case, all keys are strings, but sometimes you see array's (those are lua tables that are only using numerical keys) and then you have to use the [] syntax.

Here's some runnable code:

 

Please login or register to see this code.

As expected, it prints:

Please login or register to see this code.

In practice, you may want to add error handling, if it is not guaranteed that the answer always contains all fields. For instance, if the "data" part is missing, data.zone.id will give an error.

 

Link to comment
Share on other sites

  • 0
  • Inquirer
  • Let me first "prettify" that chunk of JSON:

     

    Please login or register to see this code.

     

     

    You can say that each level is separated by a dot, if the key is a string. In this case, all keys are strings, but sometimes you see array's (those are lua tables that are only using numerical keys) and then you have to use the [] syntax.

    Here's some runnable code:

     

    Please login or register to see this code.

    As expected, it prints:

    Please login or register to see this code.

    In practice, you may want to add error handling, if it is not guaranteed that the answer always contains all fields. For instance, if the "data" part is missing, data.zone.id will give an error.

     

    Hi Peter,

    That's exactly what I was looking for...

    I can now match the array construct with the code and I think I was close but not being a coder I get the basics wrong sometimes

    Please login or register to see this image.

    /emoticons/default_icon_sad.gif" alt=":-(" />

     

    much appreciated

    -Frank

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • another question if I may...

     

    I'm trying to loop through a number of zones and issue a GET for each zone

     

    Code is as follows

    Please login or register to see this code.

    the output is the following

    Please login or register to see this code.

    I'm trying to display the zoneNum and then the result of the http request but it seems to be cycling through the zoneNum first and then the http requests.

    I tried sleeps in case the response wasnt getting back in time but I think the answer is in the way that lua behaves

     

    Any thoughts ?
    Thanks

    frank

    Link to comment
    Share on other sites

    • 0

    I'll solve this question because... this is going to haunt you

    Please login or register to see this image.

    /emoticons/default_icon_smile.gif" alt=":-)" />

     

    There are 2 special functions on a HC2: setTimeout and the http request. These functions don't wait to get an answer. They do the request, wait for the response, then call a *function*. Maybe that explains why there is something like "success = function (....." in the code, you'll never see a function used that way with sleep() or fibaro:call... It is pretty standard Lua practice though. Also note, your answer is only processed after the main code has finished. You can test that by putting a large sleep at the end of your script and observe that you only get responses after that.

     

    There are only a few topics discussing this, because very often people seem to call only one URL and then this behavior is completely hidden (but still there)!

     

    So if you make a loop, to get 20 websites, they may get out of order. With external servers, things certainly get out of order. If this is an internal web server, things might still get out of order a few % of all cases (because of load, multithreading server side and client side).

     

    How to solve? If you really want to "process B after A" you simply call B after A. If you are not into programming, the technique has a name: "recursive programming".

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  •  

    I'll solve this question because... this is going to haunt you

    Please login or register to see this image.

    /emoticons/default_icon_smile.gif" alt=":-)" />

     

    There are 2 special functions on a HC2: setTimeout and the http request. These functions don't wait to get an answer. They do the request, wait for the response, then call a *function*. Maybe that explains why there is something like "success = function (....." in the code, you'll never see a function used that way with sleep() or fibaro:call... It is pretty standard Lua practice though. Also note, your answer is only processed after the main code has finished. You can test that by putting a large sleep at the end of your script and observe that you only get responses after that.

     

    There are only a few topics discussing this, because very often people seem to call only one URL and then this behavior is completely hidden (but still there)!

     

    So if you make a loop, to get 20 websites, they may get out of order. With external servers, things certainly get out of order. If this is an internal web server, things might still get out of order a few % of all cases (because of load, multithreading server side and client side).

     

    How to solve? If you really want to "process B after A" you simply call B after A. If you are not into programming, the technique has a name: "recursive programming".

    Please login or register to see this code.

     

    Thanks Peter...

    still learning every day ......

     

    BTW - I have been thinking about the heating control problem you were trying to help me with.

    I think something like keystroke automation or a software robot may help solve that one. I'm going to try and dive into that one sometime.. after I solve my imperihome/open remote issue and install/program my Global Cache networked IR unit.

     

    ... never running out of challenges 

    Please login or register to see this image.

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

    Link to comment
    Share on other sites

    Guest
    This topic is now closed to further replies.
    ×
    ×
    • Create New...