1.cjson.encode
cjson.decode
和System.IO.File进行json文件的读写。
m.JsonPath = 'Assets/debug_cfg.json'
local text = File.ReadAllText(m.JsonPath)
local jsonInfos = cjson.decode(text)
m.templateKeyName = jsonInfos[m.prefKeyName] or "" 20 m.templateInfo = self:stringToTable(jsonInfos[m.templateKeyName] or "")
local text = File.ReadAllText(m.JsonPath)
local jsonInfos = cjson.decode(text)
jsonInfos[m.prefKeyName] = saveName
jsonInfos[saveName] = saveStr
local saveInfo = cjson.encode(jsonInfos)
File.WriteAllText(m.addonDebugJsonPath, saveInfo)
2.
string 和 table的相互转化。 string存储在PlayerPrefs中
1 function m:serialize(obj)
2 local lua = ""
3 local t = type(obj)
4 if t == "number" then
5 lua = lua .. obj
6 elseif t == "boolean" then
7 lua = lua .. tostring(obj)
8 elseif t == "string" then
9 lua = lua .. string.format("%q", obj)
10 elseif t == "table" then
11 lua = lua .. "{"
12 for k, v in pairs(obj) do
13 lua = lua .. "[" .. self:serialize(k) .. "]=" .. self:serialize(v) .. ","
14 end
15 local metatable = getmetatable(obj)
16 if metatable ~= nil and type(metatable.__index) == "table" then
17 for k, v in pairs(metatable.__index) do
18 lua = lua .. "[" .. self:serialize(k) .. "]=" .. self:serialize(v) .. ","
19 end
20 end
21 lua = lua .. "}"
22 elseif t == "nil" then
23 return nil
24 else
25 error("can not serialize a " .. t .. " type.")
26 end
27 return lua
28 end
29
30 function m:table2string(tablevalue)
31 local stringtable = self:serialize(tablevalue)
32 return stringtable
33 end
34
35 function m:stringToTable(str)
36 local _result = loadstring("return " .. str);
37 return _result();
38 end
改变自己