2️Lua--Json模块

导入模块

local json = require("json")

只可以序列化Lua的Table不能序列化UserData自行转换

--序列化Table为json
    local demo_json = {
        UserName = "Wxdi123",
        MsgTime = os.time(),
        MsgArr = {"hello", "world"}
    }
    local write_json = json.encode(demo_json)
    log.notice("Demo json Log\n%s", write_json)
    序列化结果
    {"UserName":"Wxdi123","MsgTime":1626323666,"MsgArr":["hello","world"]}
    --反序列化json字符串到Table
    local json_str = '{"UserName":"Wxdi123","MsgTime":1626323666,"MsgArr":["hello","world"]}'
    local json_table = json.decode(json_str)
     --序列化结果是一个Table

    local user_name = json_table.UserName
    local msg_time = json_table.MsgTime
    local msg_1 = json_table.MsgArr[1] --lua的数组索引从1开始

Last updated