Lua中嵌套数组的行为

Lua中嵌套数组的行为,lua,lua-table,Lua,Lua Table,我有一段代码: json = require('dkjson') dataStr = "{}" function addEntry(position, hour, id) local data = json.decode(dataStr) or {} if not data.detections then print("create data.detections")

我有一段代码:

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)
我在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 ? 
从:

它也可以用来保存Lua数据结构,但您应该 请注意,并非每个Lua表都可以用JSON表示 标准例如,包含字符串键和 数组部分不能用JSON精确表示

印刷品

create data.detections
create data.detections[position]
create data.detections[position][hour]
{"detections":{"toto":{"28000":[11111,11111]}}}
您创建并编码了表

{detections = {toto = {[28000] = {11111, 11111}}}}
{detections = {toto = {[28000] = {22222, 22222}, ["28000"] = {11111, 11111}}}}
{detections = {toto = {["28000"] = {11111, 11111}}}}
这将为您提供json字符串

'{"detections":{"toto":{"28000":[11111,11111]}}}'
'{"detections":{"toto":{"28000":[22222,22222],"28000":[11111,11111]}}}'
当您解码这个json字符串时,您将得到一个类似于

{detections = {toto = {["28000"] = {11111, 11111}}}}
正如您在解码后看到的,28000现在是一个字符串键,而不是您编码的整数

所以当你打电话的时候

addEntry("toto", 28000, 22222)
如果小时又是一个数字,那么你将以桌子结束

{detections = {toto = {[28000] = {11111, 11111}}}}
{detections = {toto = {[28000] = {22222, 22222}, ["28000"] = {11111, 11111}}}}
{detections = {toto = {["28000"] = {11111, 11111}}}}
它同时包含numberic键和string键的值

如果再次对其进行编码,则会得到json字符串

'{"detections":{"toto":{"28000":[11111,11111]}}}'
'{"detections":{"toto":{"28000":[22222,22222],"28000":[11111,11111]}}}'
现在我们再次解码,留下桌子

{detections = {toto = {[28000] = {11111, 11111}}}}
{detections = {toto = {[28000] = {22222, 22222}, ["28000"] = {11111, 11111}}}}
{detections = {toto = {["28000"] = {11111, 11111}}}}
由于第一个
“28000”
条目
{222222222}
当然会被第二个
{11111111}
覆盖,因为同一个键不能有两个元素

下面的每个调用都会发生同样的情况。您添加了一个数字键,但在en/解码过程中丢失了它

仅当表是具有从1到n的连续整数键的序列时,才会使用数字键。所有其他表键都转换为字符串

双值是由您这样做的事实造成的

 data.detections[position][hour] = {id}
data.detections[position][hour]
nil
时,由于上述问题,在每次调用中都是如此


在向表中添加id之后,您将得到一个包含
id
的表。改为创建一个空表。

={id}
替换为
={}
用以下语句启动
addEntry
的主体:
hour=tostring(hour)
tostring创建了它!非常感谢。{id}是我在复制代码之前编写的if-then-else端的一个输入错误。谢谢,现在已经非常清楚了!