我已經在lua中序列化了一個表。lua有沒有任何函數來反序列化它?Lua中的反序列化
function dump(o)
if type(o) == 'table' then
local s = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then k = '"'..k..'"' end
s = s .. '['..k..'] = ' .. dump(v) .. ','
end
return s .. '} '
else
return tostring(o)
end
end
local people = {
{
name = "Fred",
address = "16 Long Street",
phone = "123456"
},
{
name = "Wilma",
address = "16 Long Street",
phone = "123456"
},
{
name = "Barney",
address = "17 Long Street",
phone = "123457"
}
}
file = io.open("test.lua", "a")
file:write("People:", dump(people))
這個程序的輸出是:
人:{[1] = {[ 「電話」] = 123456,[ 「名稱」] =弗雷德,[ 「地址」] =長街16, },[2] = {[「phone」] = 123456,[「name」] = Wilma, [「address」] = 16 Long Street,},[3] = {[「phone」] = 123457, 「name」] = Barney,[「address」] = 17 Long Street,},}
請推薦一種方法在lua中反序列化它。
你應該使用'(「%q」):format(o)'而不是'tostring(o)'來正確地序列化字符串。然後在你的文件的開頭用'return'替換'People:'並用'local people = dofile(「test.lua」)反序列化' –