JSON

json.getPath

使用点路径或路径数组从表中读取嵌套值。

语法

json.getPath(object: table, path: string | table, defaultValue?: any): any | nil
别名 json.getpath

参数

名称类型描述
objecttable要检查或更改的对象、表格或绘图值。
pathstring | table文件或文件夹的工作空间相对路径。
defaultValue?any当请求的路径丢失时返回后备值。

返回值

名称类型描述
pathValueany | nil请求路径处的值,或后备值。

说明

使用点路径或路径数组从表中读取嵌套值。

使用 3 个参数调用: object, path, defaultValue。参数表会说明哪些值必填,哪些值只用于细化行为。

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

示例

当路径丢失时,读取带有后备的嵌套值。

local document = {
    user = {
        profile = { name = "Guest" },
    },
}

local name = json.getPath(document, { "user", "profile", "name" }, "Unknown")
print(name)