これは再帰的にテーブルをシリアライズ。このコードの変形を使用して、テーブルからJSONを生成することができます。
これを通して、あなたのテーブルを実行している
function tprint (tbl, indent)
if not indent then indent = 0 end
local toprint = string.rep(" ", indent) .. "{\r\n"
indent = indent + 2
for k, v in pairs(tbl) do
toprint = toprint .. string.rep(" ", indent)
if (type(k) == "number") then
toprint = toprint .. "[" .. k .. "] = "
elseif (type(k) == "string") then
toprint = toprint .. k .. "= "
end
if (type(v) == "number") then
toprint = toprint .. v .. ",\r\n"
elseif (type(v) == "string") then
toprint = toprint .. "\"" .. v .. "\",\r\n"
elseif (type(v) == "table") then
toprint = toprint .. tprint(v, indent + 2) .. ",\r\n"
else
toprint = toprint .. "\"" .. tostring(v) .. "\",\r\n"
end
end
toprint = toprint .. string.rep(" ", indent-2) .. "}"
return toprint
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"
}
}
print (tprint(people))
はこれを生成します。
{
[1] = {
name= "Fred",
phone= "123456",
address= "16 Long Street",
},
[2] = {
name= "Wilma",
phone= "123456",
address= "16 Long Street",
},
[3] = {
name= "Barney",
phone= "123457",
address= "17 Long Street",
},
}
**ありがとう本当に役に立ちました** – Neenu