I have this piece of code :
json = require('dkjson')
dataStr = "{}"
function addEntry(position, hour, id)
local data = json.decode(dataStr) or {}
if not data.detections then
print("create data.detections")
data.detections = {}
end
if not data.detections[position] then
print("data.detections[position]")
data.detections[position] = {}
end
if not data.detections[position][hour] then
print("data.detections[position][hour]")
data.detections[position][hour] = {}
end
table.insert(data.detections[position][hour], id)
dataStr = json.encode(data)
print(dataStr)
end
addEntry("toto", 28000, 11111)
addEntry("toto", 28000, 22222)
addEntry("toto", 28000, 33333)
addEntry("toto", 28000, 44444)
addEntry("toto", 28000, 55555)
And I have this output on Zerobrane :
create data.detections
create data.detections[position]
create data.detections[position][hour]
{"detections":{"toto":{"28000":[11111,11111]}}}
create data.detections[position][hour]
{"detections":{"toto":{"28000":[22222,22222],"28000":[11111,11111]}}}
create data.detections[position][hour]
{"detections":{"toto":{"28000":[33333,33333],"28000":[11111,11111]}}}
create data.detections[position][hour]
{"detections":{"toto":{"28000":[44444,44444],"28000":[11111,11111]}}}
create data.detections[position][hour]
{"detections":{"toto":{"28000":[55555,55555],"28000":[11111,11111]}}}
I would have expected to have this in the final string :
```{"detections":{"toto":{"28000":[11111,22222,33333,44444,55555]}}}
Can somebody explain to me this Lua behavior ? The override instead of adding a new value, and this remaining separate first value ?
= {id}with= {}addEntrywith the following statement:hour = tostring(hour)