> For the complete documentation index, see [llms.txt](https://babyqdoc.gitbook.io/wechatdocs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://babyqdoc.gitbook.io/wechatdocs/lua-cha-jian-kai-fa/luajson-mo-kuai.md).

# Lua--Json模块

{% hint style="info" %}
导入模块

local json = require("json")

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

```
--序列化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开始
```
