[英]Does a Lua Table / Array Beautifier exist?
考慮到所有的 json、xml 格式化程序\/美化工具,我一直無法為 Lua 表\/數組找到一個?
這里的細微差別是要美化的輸出來自我認為廣泛使用的表\/數組
dump<\/code>函數 - 見下文..
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
這樣處理的一個例子,你可以自己糾正一些小事情:
function dump(o,level)
level = level or 1
if type(o) == 'table' then
local s = {}
s[1] = '{ '
for k,v in pairs(o) do
if type(k) ~= 'number' then
k = '"'..k..'"'
end
s[#s+1] = string.rep('\t',level).. '['..k..'] = ' .. dump(v, level+1) .. ','
end
s[#s+1] = string.rep('\t',level) .. '} '
return table.concat(s , "\n")
else
return tostring(o or 'nil')
end
end
local t = {[1] = nil,[2] = { ["attr"] = { [1] = code,[2] = remaining,[3] = resetdate,["remaining"] = 990,["resetdate"] = 1638614242,["code"] = 200,} ,["tag"] = success,} ,[3] = nil,["attr"] = { } ,["tag"] = prowl,}
print (dump(t))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.