將 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