ToJSON: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Undo revision 29622 by Castillo (talk))
(ToJSON was described wrong, it only grabs one argument, updated syntax, description and example)
Line 4: Line 4:
Available client side in 1.2 and onwards
Available client side in 1.2 and onwards
}}
}}
This function converts one or more variables into a [[JSON]] encoded string. You can use this to store the data and then load it again using [[fromJSON]].
This function converts '''one''' variable (preferably a table) into a [[JSON]] encoded string. You can use this to store the data and then load it again using [[fromJSON]].


==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 -->
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
string toJSON( var arguments ... )
string toJSON( var value )
</syntaxhighlight>  
</syntaxhighlight>  


===Required Arguments===  
===Required Arguments===  
*'''arguments:''' A list of arguments 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.
*'''var:''' An arguments 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.


===Returns===
===Returns===
Line 19: Line 19:


==Example==  
==Example==  
This example shows how you can encode an array. The string json should equal ''"["cat", "mouse", {cat:"hungry",mouse:"food"}]" after executed.
This example shows how you can encode an array. The string json should equal ''"[ { "1": "dogs", "mouse": "food", "cat": "hungry", "birds": 4 } ]" after executed.
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local json = toJSON ( "cat", "mouse", {cat="hungry",mouse="food"} )
local json = toJSON ( { "dogs", cat = "hungry", mouse = "food", birds = 4 } )
</syntaxhighlight>
</syntaxhighlight>



Revision as of 19:00, 29 March 2012

Available client side in 1.2 and onwards This function converts one variable (preferably a table) into a JSON encoded string. You can use this to store the data and then load it again using fromJSON.

Syntax

string toJSON( var value )

Required Arguments

  • var: An arguments 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.

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

Requirements

This template will be deleted.

See Also