strtable.lua 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. -- 字符串转变为 table表
  2. local strtable = {}
  3. function strtable.StrToTable(str)
  4. -- if str == nil or type(str) ~= "string" then
  5. if str == nil then
  6. return
  7. end
  8. local z = {load("return " .. str)}
  9. return z
  10. end
  11. -- table表转换为str
  12. function strtable.ToStringEx(value)
  13. if type(value)=='table' then
  14. return strtable.TableToStr(value)
  15. elseif type(value)=='string' then
  16. return "\""..value.."\""
  17. else
  18. return tostring(value)
  19. end
  20. end
  21. --使用的时候是这个
  22. function strtable.TableToStr(t)
  23. if t == nil then return "" end
  24. if type(t) ~= 'table' then return t end
  25. local retstr= "{"
  26. local i = 1
  27. for key,value in pairs(t) do
  28. local signal = ","
  29. if i==1 then
  30. signal = ""
  31. end
  32. if key == i then
  33. retstr = retstr..signal..strtable.ToStringEx(value)
  34. else
  35. if type(key)=='number' or type(key) == 'string' then
  36. retstr = retstr..signal.."["..strtable.ToStringEx(key).."]="..strtable.ToStringEx(value)
  37. else
  38. if type(key)=='userdata' then
  39. retstr = retstr..signal.."*s"..TableToStr(getmetatable(key)).."*e".."="..strtable.ToStringEx(value)
  40. else
  41. retstr = retstr..signal..key.."="..strtable.ToStringEx(value)
  42. end
  43. end
  44. end
  45. i = i+1
  46. end
  47. retstr = retstr.."}"
  48. return retstr
  49. end
  50. return strtable