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

Notes

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('{"娘子":"啊哈","你好":"世界"}')))
print(table.deep_dump(json.decode('[]')))
print(table.deep_dump(json.decode('[1, 0, 0, "4695100"]')))
--
sys.alert(print.out())

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

-- Use json.decode to convert Unicode escapes to text
sys.alert(json.decode([["\u82cf\u6cfd"]]))

Note: Uses sys.alert