将 JSON 字符串转换成 Lua 值 (json.decode)
声明
值, 错误信息 = json.decode(JSON文本)
参数
- JSON 文本
文本型,需要转换成表的 json 文本
返回值
- 值
表型 或 文本型 或 数值型 或 布尔型 或 json.null 或 nil,转换成功则返回一个与 json 字符串结构相对应的 lua 值,否则返回 nil - 错误信息
文本型 或 nil, 转换失败 值 为 nil 的情况下,这个返回值则是具体的错误信息
说明
将 json 文本转换成 lua 的中对应的数据值
该函数可在 XUI 中使用
示例
print(json.decode('true'))
print(json.decode('17'))
print(json.decode('"哈哈"'))
print(json.decode('null'))
print(json.decode(''))
print(table.deep_dump(json.decode('{}')))
print(table.deep_dump(json.decode('{"name":"alice","hello":"world"}')))
print(table.deep_dump(json.decode('[]')))
print(table.deep_dump(json.decode('[1, 2, 3, "foo"]')))
--
sys.alert(print.out())
注:上述代码中使用了非本章函数 table.deep_dump 、print.out、sys.alert
-- 使用 json.decode 将 \uXXXX 形式的 Unicode 转义还原为文字
sys.alert(json.decode([["\u4f60\u597d"]])) -- 输出:你好
注:上述代码中使用了非本章函数 sys.alert