Lua:如何将字符串反序列化为多维表?

问题描述 投票:2回答:1

我目前无法使用此功能:我有一个函数,需要一个多维表并将其转换为字符串。格式与lua代码中的表格相同。现在我必须扭转它-目标是取回相同的多维表。我已经找到了用于从表中获取字符串的功能-这是相反的吗?

    toReturn = "{"
    for ind, val in pairs(tbl) do
        if type(val) == "table" then
            toReturn = toReturn .. (type(ind) == "number" and "" or ind .. "=") .. tableAsString(val) .. ","
        else
            local newVal
            if type(val) == "string" then
                newVal = "\"" .. val .. "\""
            else
                newVal = val
            end
            toReturn = toReturn .. (type(ind) == "number" and "" or ind .. "=") .. newVal .. ","
        end
    end

    toReturn = toReturn:sub(1,-2) .. "}" -- remove trailing "," and close table

    return toReturn
end
lua deserialization
1个回答
1
投票

Nifim的答案非常有效,我的问题已解决。该表可以使用

读取
loadstring(serializedTable)()

再次感谢!

© www.soinside.com 2019 - 2024. All rights reserved.