Source: http://blog.sina.com.cn/s/blog_991afe570101rkfh.html
Thanks, the original author's selfless dedication!
Implementation ideas:
Table tables------> converted to strings (using the JSON library here)-------> Store---------> Read
To achieve the purpose:
Solve string stitching problems
The simplest way to save table tables to files, game archives, game levels, etc.
First step: Convert Table Table to JSON-formatted string
Import Header File
--2.2.5 's quick version has completed the initialization of the JSON module when the framework is loaded, so there is no need to do the import header file--local JSON = require ("Framework.shared.json")
Create a table
Local t = {} t.test = "Okok" t.net = "Okok" T.inner = {} t.inner.test = "OKOK8888"--mainly implemented using the following two methods for local Wirtjson =json.encode (t)--Convert to JSON format local Readjson =json.decode (Wirtjson)--Go to the original format before storage
Note: The following two methods can be obtained to the current computer on the root of the project
1, Device.writablepath
2 , Device.cachepath
The second step: write to the file, store and read
The first approach: the LUA IO library
Save the file to the current project root directory:
Local test = assert (Io.open (Device.writablepath. "/formation.lua", "W")) Test:write (Wirtjson) test:close ()
To read the entire file:
Local test = Io.open (Device.writablepath. "/formation.lua", "R") Local readjson= test:read ("*a") local table =json.decode (Readjson) test:close ()
The second method: Ccuserdefault
Save the file to the current project root directory:
Ccuserdefault:shareduserdefault (): Setstringforkey ("SaveFightArray1", Wirtjson) Ccuserdefault:shareduserdefault () : Flush ()
To read the entire file:
Local ecord1 = Ccuserdefault:shareduserdefault (): Getstringforkey ("SaveFightArray1") local table =json.decode (record1 )
Lua data is stored as table tables