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

json.decode


Question

Posted

Trying to decode this json string but without success.

Please login or register to see this code.

Should it be something like this?

Please login or register to see this code.

6 answers to this question

Recommended Posts

  • 0
Posted

Yes, at least according to me.

By the way, do you know if you can create global functions in a scene? To only be used by local calls from other scenes.

Like this.

Scene1:

--[[

%% properties

%% globals

--]]

global function updateDevice(deviceId, successCallback, errorCallback)

local http = net.HTTPClient()

http:request('http://127.0.0.1:11111/api/devices/' .. deviceId, {

options = {

method = 'GET'

},

success = successCallback,

error = errorCallback

})

end

Scene2:

--[[

%% properties

%% globals

--]]

deviceId = updateDevice(1, function(resp)

print("Status: " .. resp.status)

local d = json.decode(resp.data)

print(d.name)

end,

function(err)

print('error' .. err)

end

)

[ Added: 2015-01-09, 17:15 ]

btw, if you get nothing... try index 1

  • 0
  • Inquirer
  • Posted

    its the square brackets at first and last position that gives error.....

    • 0
    Posted

    Sure, but it was just an example. Is it possible to create like "global" scene like scene1 only carrying global functions to be called from separate scenes?

    • 0
  • Inquirer
  • Posted

    Need to remove first and last brackets.

    Please login or register to see this code.

    Complete code

    Please login or register to see this code.

    • 0
    Posted

    There are several issues with the json.decode function. It won't decode a json string which uses backspace as escape character - it just returns an error (and it doesn't even return the right error). I've been trying to inject some data into a virtual device with some lua in it. In the json format, the lua code is riddled with escape characters so it is impossible to avoid - and I can't make this scene run in any way (except constructing an array which is 17 miles long) because json.decode wont transform the data into an array.

    • 0
    Posted

     

    Please login or register to see this link.

     

    Join the conversation

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

    Guest
    Answer this question...

    ×   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...