FromJSON: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
No edit summary
(Removed "Needs Checking", fixed example)
Line 1: Line 1:
__NOTOC__  
__NOTOC__  
{{Server client function}}
{{Server client function}}
{{Needs_Checking|Example 1 does not work. FromJSON is not compatible with JSON objects such as "cat:5, mouse:1". You might only use JSON arrays instead or move the JSON formatting to PHP --[[User:-ffs-Sniper|-ffs-Sniper]] 20:39, 15 March 2012 (UTC)}}
{{New feature/item|3.0120|1.2||
{{New feature/item|3.0120|1.2||
Available client side in 1.2 and onwards
Available client side in 1.2 and onwards
Line 17: Line 16:
===Returns===
===Returns===
Returns variables read from the JSON string.
Returns variables read from the JSON string.
'''Note:''' Indices of a JSON object such as "1": "cat" are being returned as [[string]], not as [[int]]eger.


==Example==  
==Example==  
This should make data equal: {1="cat", 2="mouse", 3=5, 4=null, 5={cat=5, mouse=1}} (untested)
This makes data equal: ''{ ["1"] = "cat", ["2"] = "mouse", ["3"] = 5, ["4"] = null, ["cat"] = 5, ["mouse"] =1 }''
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
local data = fromJSON("[\"cat\", \"mouse\", 5, null, {cat:5, mouse:1}]")
local data = fromJSON ( '[ { "1": "cat", "2": "mouse", "3": 5, "4": null, "cat":5, "mouse":1 } ]' )
</syntaxhighlight>
</syntaxhighlight>



Revision as of 19:39, 29 March 2012

Available client side in 1.2 and onwards 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.

Example

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

Example 2

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

Requirements

This template will be deleted.

See Also