lua自定义功能模块table类型转string类型
1 local _T2S = {} 2 3 function _T2S.ToStringEx(value) 4 if type(value)=='table' then 5 return _T2S.TableToStr(value) 6 elseif type(value)=='string' then 7 return '\"'..value..'\"' 8 else 9 return tostring(value) 10 end 11 end 12 13 function _T2S.TableToStr(t) 14 if t == nil then return "" end 15 local retstr= "{" 16 17 local i = 1 18 for key,value in pairs(t) do 19 local signal = "," 20 if i==1 then 21 signal = "" 22 end 23 24 if key == i then 25 retstr = retstr..signal.._T2S.ToStringEx(value) 26 else 27 if type(key)=='number' or type(key) == 'string' then 28 retstr = retstr..signal.._T2S.ToStringEx(key)..":".._T2S.ToStringEx(value) 29 else 30 if type(key)=='userdata' then 31 retstr = retstr..signal.."*s"..TableToStr(getmetatable(key)).."*e"..":".._T2S.ToStringEx(value) 32 else 33 retstr = retstr..signal..key..":".._T2S.ToStringEx(value) 34 end 35 end 36 end 37 38 i = i+1 39 end 40 41 retstr = retstr.."}" 42 return retstr 43 end 44 45 return _T2S
1 function StrToTable(str) 2 if str == nil or type(str) ~= "string" then 3 return 4 end 5 6 return loadstring("return " .. str)() 7 end
o(=•ェ•=)m纸上得来终觉浅,绝知此事要躬行o(=•ェ•=)m