json

Top  Previous  Next

Json provides function to do JSON encoding and decoding.

Encode

jsonString = json.encode ( tableToEncode )

 

Name

Type

Description

tableToEncode

table

Table to encode to json

jsonString

string

JSON representation of tableToEncode

Example

json = require('json')

print( json.encode ( { a=10; b=20; c= { sub1= 2; sub2= 3 } } ) )

 

This will print {"a":10,"c":{"sub2":3,"sub1":2},"b":20}

Decode

resultTable = json.decode ( jsonString )

 

Name

Type

Description

resultTable

table

Table decoded from JSON string

jsonString

string

JSON representation of resultTable

Example

json = require('json')

table.print( json.decode ( '{"a":10,"c":{"sub2":3,"sub1":2},"b":20}' ))

 

This will print:

{ -- #0

   ["a"] = 10,

   ["c"] = { -- #1

       ["sub2"] = 3,

       ["sub1"] = 2,

   } -- #1,

   ["b"] = 20,

} -- #0