cocos2d-x lua table与json的转换

本文介绍如何在cocos2d-x3.6版本中使用json库实现Lua表与json数据的相互转换。通过示例代码展示了创建Lua表、将其编码为json字符串,再将json字符串解码回Lua表的过程,并记录了转换所用的时间。

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

转载于:https://www.cnblogs.com/songcf/p/4556773.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值