json
一个内置于每个宏的小型 JSON 编码器/解码器。它以全局表 json 的形式提供,与 Request 模块配合使用很方便。
方法
| 方法 | 描述 |
|---|---|
json.encode(value) | 将 Lua 值转换为 JSON 文本 |
json.decode(text) | 将 JSON 文本转换为 Lua 值 |
示例
local body = json.encode({ name = "John", age = 30 })
local response = Request("https://api.example.com/users")
:setHeader("Content-Type", "application/json")
:setBody(body)
:post()
local answer = json.decode(response:getData())
toast("Created user " .. answer.id)