<< Click to Display Table of Contents >> Navigation: Modbus Universal MasterOPC Server > Lua 5.1 Reference Manual > Function library > JSON > JSON |
The JSON tree contains functions for working with JSON files - parsing and generating files of this format.
Converts a table to a JSON string.
Input parameters: table
Output parameters: string in JSON format, or nil if an error occurred during processing.
Example
--create table
t = {
["name1"] = "value1", --element of table name1, contains string value1
["name2"] = { 1, false, true, 23.54, "string" }, --table element name2 containing the nested table
}
local encode = json.Encode(t); --convert
--result in encode - {"name1":"value1","name2":[1,false,true,23.54,"string"]}
Converts the passed JSON format string to a table.
Input parameters: JSON string.
Output parameters: table or nil if an error occurred during processing.
Пример
encode="{"name1":"value1","name2":[1,false,true,23.54,"string"]}" --example input string
t1 = json.Decode(encode); --convert
if (t1==nil) return; --check successful conversion
--out table values
server.Message(t1.name1); --"value1"
server.Message(t1.name2[1]); --1
server.Message(t1.name2[2]); --false
server.Message(t1.name2[3]); --true
server.Message(t1.name2[4]); --23.54
server.Message(t1.name2[5]); --"string"