FromJSON: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
 
(25 intermediate revisions by 15 users not shown)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Server client function}}This function parses a JSON formatted string into variables.
{{Server client function}}
This function parses a [[JSON]] formatted string into variables. You can use [[toJSON]] to encode variables into a JSON string that can be read by this function.


==Syntax==  
==Syntax==  
Line 13: Line 14:
Returns variables read from the JSON string.
Returns variables read from the JSON string.


==Example==
'''Note:''' Indices of a JSON object such as "1": "cat" are being returned as [[string]], not as [[int]]eger.


==Examples==
This makes data equal: ''{ ["1"] = "cat", ["2"] = "mouse", ["3"] = 5, ["4"] = null, ["cat"] = 5, ["mouse"] =1 }''
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
--Needs example
local data = fromJSON ( '[ { "1": "cat", "2": "mouse", "3": 5, "4": null, "cat":5, "mouse":1 } ]' )
</syntaxhighlight>
 
This shows how to extract data from JSON:
<syntaxhighlight lang="lua">
local name, weapon, ammo = fromJSON("[\"Desert Eagle\", 24, 147]")
</syntaxhighlight>
</syntaxhighlight>


==See Also==
==See Also==
{{Server_functions}}
{{Server_functions}}
[[Category:Needs_Example]] <!-- leave this until the example is completed. -->
[[Category:Incomplete]] <!-- leave this unless you complete the function -->

Latest revision as of 13:36, 8 June 2025

This function parses a JSON formatted string into variables. You can use toJSON to encode variables into a JSON string that can be read by this function.

Syntax

var fromJSON ( string json )

Required Arguments

  • json: A JSON formatted string

Returns

Returns variables read from the JSON string.

Note: Indices of a JSON object such as "1": "cat" are being returned as string, not as integer.

Examples

This makes data equal: { ["1"] = "cat", ["2"] = "mouse", ["3"] = 5, ["4"] = null, ["cat"] = 5, ["mouse"] =1 }

local data = fromJSON ( '[ { "1": "cat", "2": "mouse", "3": 5, "4": null, "cat":5, "mouse":1 } ]' )

This shows how to extract data from JSON:

local name, weapon, ammo = fromJSON("[\"Desert Eagle\", 24, 147]")

See Also