|string||The JSON format string that you are passing to the function for decoding|
Returns: ds_map id or -1 if it fails
With this function, you can decode a piece of JSON and convert it into a ds_map, ready for use in GameMaker: Studio. If the JSON to be decoded requires a hierarchy of lists and maps within the central ds_map, these are decoded too and also created for you, using the following rules (note that these rules apply to the top-level structure only):
- Json is a single value - returns a ds_map with a single entry "default" that is the value
- Json is an array of objects or values - returns a ds_map with a single entry "default" that is a ds_list of the objects or values
- Json is an object - returns a ds_map that has the object entries in it
NOTE: When decoding arrays, there is a map with the
key "default" ONLY when an array is the top level structure, and
ONLY for that top-level array. Internal lists decode directly to
ds_lists without being enclosed in a ds_map.
Normally you would know what keys the JSON decodes to, but if not then you can use the ds_map_size, ds_map_find_first and ds_map_find_next functions to parse the map and get the necessary information.
NOTE: GameMaker: Studio creates the necessary ds_maps and lists from the JSON, and for cleaning up you only need to delete the top level map or list and GameMaker: Studio will automatically delete from memory all the maps and lists underneath.
var resultMap = json_decode(requestResult);
var list = ds_map_find_value(resultMap, "default");
var size = ds_list_size(list);
for (var n = 0; n < ds_list_size(list); n++;)
var map = ds_list_find_value(list, n);
var curr = ds_map_find_first(map);
global.Name[n] = ds_map_find_value(map, "name");
curr = ds_map_find_next(map, curr);
The above code will decode a JSON string and parse it to generate a global array.