toJSON: Difference between revisions

From Bohemia Interactive Community
Jump to navigation Jump to search
No edit summary
Tag: Reverted
(Small page overhaul)
 
(2 intermediate revisions by 2 users not shown)
Line 3: Line 3:
|game1= arma3
|game1= arma3
|version1= 2.18
|version1= 2.18
|branch= dev


|gr1= Strings
|gr1= Strings


|descr= Serialise the passed data to {{Link|https://en.wikipedia.org/wiki/JSON|JSON}} format.
|descr= Converts data to {{Link|https://en.wikipedia.org/wiki/JSON|JSON}}.


|s1= [[toJSON]] data
|s1= [[toJSON]] data


|p1= data: can be one of:
|p1= data - Only certain [[Data Types]] are supported:
* [[HashMap]]: JSON object
* [[Boolean]] - serialized as JSON boolean
* [[Array]]: JSON array
* [[Number]] - serialized as JSON number (floating-point)
* [[Number]]: JSON number (float)
* [[String]] - serialized as JSON string
* [[String]]: JSON string
* [[Array]] - Serialized as JSON array. Unsupported elements (e.g. [[Object]]s) are converted to JSON null.
* [[Boolean]]: JSON bool (true/false, not 1/0)
* [[HashMap]] - Serialized as JSON object. Only [[String]] keys are supported, other keys are ignored. Unsupported values (e.g. [[Object]]s) are converted to JSON null.
* [[nil]]: JSON null {{Feature|important|The [[nil]] value can only be passed as a member of [[Array]]/[[HashMap]] and cannot be passed directly as the command would simply not execute.}}
* [[nil]] - serialized as JSON null {{Feature|important|The [[nil]] value can only be passed as a member of an [[Array]] or [[HashMap]]. It cannot be passed directly as the command would simply not execute.}}


|r1= [[String]] - JSON result
|r1= [[String]] - JSON string


|x1= <sqf>
|x1= <sqf>
private _result = toJSON ["this", "is", "an", "array"];
private _result = toJSON ["this", "is", "an", "array"];
</sqf>
</sqf>
result:
Result:
<syntaxhighlight lang="json">
<syntaxhighlight lang="json">
["this","is","an","array"]
["this","is","an","array"]
Line 39: Line 37:
toJSON _hashMap;
toJSON _hashMap;
</sqf>
</sqf>
result:
Result (formatted for readability):
<syntaxhighlight lang="json">
<syntaxhighlight lang="json">
{"bKey":true,"sKey":"Hello there","oKey":null,"nkey":42,hKey:{"key1":"General Kenobi","key2":true,"yolo":"Hello it's me"}}
{
"bKey": true,
"sKey": "Hello there",
"oKey": null,
"nkey": 42,
"hKey": {
"key1": "General Kenobi",
"key2": true,
"yolo": "Hello it's me"
}
}
</syntaxhighlight>
</syntaxhighlight>


Line 48: Line 56:
</sqf>
</sqf>


|seealso= [[fromJSON]] [[toString]] [[str]] [[parseSimpleArray]]
|seealso= [[fromJSON]] [[toString]] [[str]]
}}
}}

Latest revision as of 17:34, 4 November 2024

Hover & click on the images for description

Description

Description:
Converts data to JSON.
Groups:
Strings

Syntax

Syntax:
toJSON data
Parameters:
data - Only certain Data Types are supported:
  • Boolean - serialized as JSON boolean
  • Number - serialized as JSON number (floating-point)
  • String - serialized as JSON string
  • Array - Serialized as JSON array. Unsupported elements (e.g. Objects) are converted to JSON null.
  • HashMap - Serialized as JSON object. Only String keys are supported, other keys are ignored. Unsupported values (e.g. Objects) are converted to JSON null.
  • nil - serialized as JSON null
    The nil value can only be passed as a member of an Array or HashMap. It cannot be passed directly as the command would simply not execute.
Return Value:
String - JSON string

Examples

Example 1:
private _result = toJSON ["this", "is", "an", "array"];
Result:
["this","is","an","array"]
Example 2:
private _hashMap = createHashMap; _hashMap set ["bKey", true]; _hashMap set ["sKey", "Hello there"]; _hashMap set ["oKey", player]; _hashMap set ["nKey", 42]; _hashMap set ["hKey", ["key1", "key2", "yolo"] createHashMapFromArray ["General Kenobi", true, "Hello it's me"]]; toJSON _hashMap;

Result (formatted for readability):

{
	"bKey": true,
	"sKey": "Hello there",
	"oKey": null,
	"nkey": 42,
	"hKey": {
		"key1": "General Kenobi",
		"key2": true,
		"yolo": "Hello it's me"
	}
}
Example 4:
private _jsonPlayer = toJSON player; // empty string - Object is not supported

Additional Information

See also:
fromJSON toString str

Notes

Report bugs on the Feedback Tracker and/or discuss them on the Arma Discord or on the Forums.
Only post proven facts here! Add Note