ToJSON: Difference between revisions
Jump to navigation
Jump to search
(Add ru page) |
Fernando187 (talk | contribs) (Remove obsolete Requirements section) |
||
(4 intermediate revisions by 3 users not shown) | |||
Line 3: | Line 3: | ||
This function converts a '''single''' value (preferably a Lua table) into a [[JSON]] encoded string. You can use this to store the data and then load it again using [[fromJSON]]. | This function converts a '''single''' value (preferably a Lua table) into a [[JSON]] encoded string. You can use this to store the data and then load it again using [[fromJSON]]. | ||
{{Important Note|Due to technical limitations (partly of json-c) the stringified keys will be truncated to the first 255 characters}} | |||
{{ Warning| When using [[toJSON]] for submitting data using [[fetchRemote]] for example, make sure to use '''string.sub(data, 2, -2)''' to remove the brackets as many APIs will not understand the request }} | |||
==Syntax== | ==Syntax== | ||
<!-- NOTE: don't use 'special' names for variable names, e.g. you shouldn't be writing things like 'player player, vehicle vehicle', instead write something like 'player thePlayer, vehicle vehicleToGetInto'. This is less confusing and prevents the syntax highlighting being odd --> | <!-- NOTE: don't use 'special' names for variable names, e.g. you shouldn't be writing things like 'player player, vehicle vehicle', instead write something like 'player thePlayer, vehicle vehicleToGetInto'. This is less confusing and prevents the syntax highlighting being odd --> | ||
Line 32: | Line 34: | ||
local json = toJSON ( { "dogs", cat = "hungry", mouse = "food", birds = 4 } ) | local json = toJSON ( { "dogs", cat = "hungry", mouse = "food", birds = 4 } ) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
==See Also== | ==See Also== |
Latest revision as of 15:32, 7 November 2024
This function converts a single value (preferably a Lua table) into a JSON encoded string. You can use this to store the data and then load it again using fromJSON.
Important Note: Due to technical limitations (partly of json-c) the stringified keys will be truncated to the first 255 characters |
Syntax
string toJSON ( var value [, bool compact = false ][, string prettyType = "none" ] )
Required Arguments
- var: An argument of any type. Arguments that are elements will be stored as element IDs that are liable to change between sessions. As such, do not save elements across sessions as you will get unpredictable results.
Optional Arguments
NOTE: When using optional arguments, you might need to supply all arguments before the one you wish to use. For more information on optional arguments, see optional arguments.
- compact: a boolean representing whether the string will contain whitespaces. To remove whitespaces from JSON string, use true. String will contain whitespaces per default.
- prettyType: a type string from below:
- none
- spaces
- tabs
Returns
Returns a JSON formatted string.
Example
This example shows how you can encode an array. The string json should equal "[ { "1": "dogs", "mouse": "food", "cat": "hungry", "birds": 4 } ]" after executed.
local json = toJSON ( { "dogs", cat = "hungry", mouse = "food", birds = 4 } )