cocos2d-x lua table与json的转换

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
posted @ 2015-06-06 15:57  、拂晓  阅读(4705)  评论(0编辑  收藏  举报