JSON

json.decode

解析 JSON 字符串并返回匹配的 Luau 值。

语法

json.decode(json: string, options?: DecodeOptions): any

参数

名称类型描述
jsonstring要解析、格式化、缩小或验证的 JSON 字符串。
options?DecodeOptions控制操作行为方式的可选设置表。

返回值

名称类型描述
decodedany解码的 Luau 值或解码的字节字符串。

说明

解析 JSON 字符串并返回匹配的 Luau 值。

使用 2 个参数调用: json, options。参数表会说明哪些值必填,哪些值只用于细化行为。

返回 decoded (any)。使用返回表区分成功值、nil 结果和可恢复错误。

示例

将有效的 JSON 解码为 Luau 值。

local value = json.decode('{"name":"Real","enabled":true}')

print(value.name)
print(value.enabled)

类型

DecodeOptions

控制 JSON 解析行为。

useNull? boolean 使用库的 null 标记而不是 nil 来表示 JSON null。maxDepth? integer 设置最大嵌套深度,范围为 1 到 4096。