您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x lua table与json的转换

2015-06-06 15:54 513 查看

cocos2d-x lua table与json的转换

version: cocos2d-x 3.6

1.引入json库

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


2.使用json

function testJson()
local beginTime = os.time()

local testTable = {}
--            [
--                {
--                    "UserId": "1234567890",
--                    "Name": "test1",
--                    "Icon": "www.google.com"
--                },
--                {
--                    "UserId": "1234567890",
--                    "Name": "test2",
--                    "Icon": "www.google.com"
--                }
--            ]
for i = 1, 100 do
table.insert(testTable, {["UserId"]="123", ["Name"]="test1", ["Icon"]="www.google.com"})
end

-- encode
local jsonData = json.encode(allUserData)
print("user data json:\n" .. jsonData)

-- decode
testTable = json.decode(jsonData)

local endTime = os.time()
print("test time: " .. tostring(endTime - beginTime))
end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  cocos2d-x lua json table 转换