Skip to main content

Decode JSON string to Lua value (json.decode)

Declaration​

value, err = json.decode(jsonText)

Parameters​

  • jsonText
    String. JSON text to be converted to a Lua value/table.

Returns​

  • value
    Table or String or Number or Boolean or json.null or nil. A Lua value corresponding to the JSON structure on success; otherwise nil.
  • err
    String or nil. Error message when conversion fails (i.e., when value is nil).

Description​

Converts JSON text into the corresponding Lua value.
This function can be used in XUI.

Example​

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())

Note: Uses table.deep_dump, print.out, sys.alert

-- Use json.decode to convert \uXXXX Unicode escapes to text
sys.alert(json.decode([["\u4f60\u597d"]])) -- output: 你好

Note: Uses sys.alert