cocos2d-x lua table数据存储

时间:2023-01-01 11:37:38

cocos2d-x lua table数据存储

version: cocos2d-x 3.6

1. 将table转为json

http://blog.csdn.net/songcf_faith/article/details/46389157

http://www.cnblogs.com/songcf/p/4556773.html

1) 引入json库

require("src/cocos/cocos2d/json")

2) 使用json

-- table转json
local jsonData = json.encode(tableData)
-- json转table
local testTable = json.decode(jsonData)

2. 数据持久化

1) 保存json数据

local tableData = json.encode(TABLE_DATA)
local file = io.open(SAVE_PATH,"wb")
file:write(tableData)
io.close(file)

2) 读取json数据

 local file = io.open(SAVE_PATH, "rb")
local readjson= file:read("*a") --read all
file:close()
local tableData = json.decode(readjson)