语法
json.validate(json: string, options?: DecodeOptions): boolean | string | nil | number | nil参数
| 名称 | 类型 | 描述 |
|---|---|---|
json | string | 要解析、格式化、缩小或验证的 JSON 字符串。 |
options? | DecodeOptions | 控制操作行为方式的可选设置表。 |
返回值
| 名称 | 类型 | 描述 |
|---|---|---|
valid | boolean | 当 JSON 字符串有效时为 True。 |
errorMessage | string | nil | 加载或解析失败时返回错误信息,否则返回nil。 |
errorPosition | number | nil | 解析失败的位置,如果没有发生解析错误则为nil。 |
说明
验证 JSON 并在解析失败时返回错误详细信息。
使用 2 个参数调用: json, options。参数表会说明哪些值必填,哪些值只用于细化行为。
返回 valid (boolean), errorMessage (string | nil), errorPosition (number | nil)。使用返回表区分成功值、nil 结果和可恢复错误。
示例
验证 JSON 并在失败时报告解析器位置。
local valid, message, position = json.validate('{"value":42}')
if not valid then
warn(message, position)
end类型
DecodeOptions
控制 JSON 解析行为。
useNull? boolean 使用库的 null 标记而不是 nil 来表示 JSON null。maxDepth? integer 设置最大嵌套深度,范围为 1 到 4096。