I have a script running that checks if mobile devices are present on my WiFi network. For this I use the API of my Unifi controller. This API returns a string that I search for certain MAC adreses. Everything works fine.
Now I want to do more things with the data I receive from the controller. This is JSON and is converted into a LUA table by DzVents. I am able to read the data from the table:
By changing t from 1 to n in this example I am able to read all the mac adreses of devices that have a connection on my WiFi.
What I don't know is how many records there are (n). So I want to check the max of t; what is n.
Delfuego wrote: ↑Wednesday 04 November 2020 10:15
Thank you! The simple #device.json.data did the job for me.
I also tried your LUA way. I'm not sure what to do with the result. Below you see first the result of the simple code (41) and next the LUA way.
I suppose that the LUA way can deal with missing numbers?!? Maybe you can elaborate on this a little bit more?
-- Standard Lua method to walk a one dimensionable table with consecutive integer values from 1..n as keys (aka Array)
for index, value in ipairs(device.json.data) do
print(index .. ': '.. tostring(value) )
end
-- Standard Lua method to walk a one dimensionable table with other type keys (strings, non consecutive numbers, subtables, functions, etc..)
for key, value in pairs(device.json.data) do
print(tostring(key) .. ': '.. tostring(value) )
end
-- Funtion to print structure and content of any table
local function dumpTable(t, level)
local level = level or "> "
for attr, value in pairs(t or {}) do
if (type(value) ~= 'function') then
if (type(value) == 'table') then
print(level .. attr .. ':')
dumpTable(value, level .. ' ')
else
print(level .. attr .. ': ' .. tostring(value))
end
else
print(level .. attr .. '()')
end
end
end
-- Call the function dumpTable
dumpTable(device.json)