json.decode()
将 JSON 文本解码为 Lua 值。
签名
json.decode(text: string): any
参数
| 参数 | 类型 | 描述 |
|---|---|---|
| text | string | 要解析的 JSON 文本 |
返回值
解码后的值:对象和数组为表,其他为字符串、数字、布尔值或 nil。
描述
无效 JSON 会抛出错误。当服务器可能返回其他内容(例如 HTML 错误页面)时,请用 pcall() 包裹网络输入。
示例
local response = Request("https://api.example.com/user/1"):get()
local ok, user = pcall(function()
return json.decode(response:getData())
end)
if ok then
toast("Hello " .. user.name)
end