【问题标题】:Behavior of nested arrays in LuaLua 中嵌套数组的行为
【发布时间】:2021-04-11 18:53:21
【问题描述】:

我有这段代码:

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 ? 

【问题讨论】:

  • = {}替换= {id}
  • addEntry 的正文以以下语句开头:hour = tostring(hour)
  • tostring 成功了!谢谢 ! {id} 是我在复制代码之前写的 if then else end 留下的错字。

标签: lua lua-table


【解决方案1】:

来自dkjson documentation

它也可以用来保存 Lua 数据结构,但你应该 注意不是每个 Lua 表都可以用 JSON 表示 标准。例如同时包含字符串键和 数组部分不能完全用 JSON 表示。

addEntry("toto", 28000, 11111)

打印

create data.detections
create data.detections[position]
create data.detections[position][hour]
{"detections":{"toto":{"28000":[11111,11111]}}}

您创建并编码了表格

{detections = {toto = {[28000] = {11111, 11111}}}}

给你json字符串

'{"detections":{"toto":{"28000":[11111,11111]}}}'

当你解码这个 json 字符串时,你会得到一个类似的 lua 表

{detections = {toto = {["28000"] = {11111, 11111}}}}

解码后你看到 28000 现在是一个字符串键,而不是你编码的整数。

所以当你打电话时

addEntry("toto", 28000, 22222)

小时又是一个数字,你会得到表格

{detections = {toto = {[28000] = {22222, 22222}, ["28000"] = {11111, 11111}}}}

包含数字键和字符串键的值。

如果你再次编码,你会得到 json 字符串

'{"detections":{"toto":{"28000":[22222,22222],"28000":[11111,11111]}}}'

现在我们再次解码,留下表格

{detections = {toto = {["28000"] = {11111, 11111}}}}

因为第一个 "28000" 条目 {22222, 22222} 当然会被第二个 {11111, 11111} 覆盖,因为我们不能有两个元素用于同一个键。

现在,您接下来的每个调用都会发生同样的事情。您添加一个数字键并在编码/解码过程中丢失它。

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

double 值是由于你这样做造成的

 data.detections[position][hour] = {id}

data.detections[position][hour]nil 时,由于上述问题,每次调用都为真。

之后,当您将 id 添加到表中时,您最终会得到一个包含 id 两次的表。而是创建一个空表。

【讨论】:

    猜你喜欢
    • 2013-10-20
    • 2016-07-01
    • 2011-09-17
    • 1970-01-01
    • 2018-11-21
    • 2014-08-12
    • 2015-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多