json_decode

This function decodes a JSON string and converts it into a DS Map, ready for use in GameMaker.

重要 This function - while still valid - has been superseded by the function json_parse, and we recommend that you only use this function for legacy support.

GameMaker creates the necessary DS maps and lists from the JSON, and for cleaning up you only need to delete the top-level map and GameMaker will automatically delete from memory all the maps and lists underneath.
If the JSON to be decoded requires a hierarchy of lists and maps within the central DS map, these are also decoded and created for you, using the following rules (note that these rules apply to the top-level structure only):

Usage Notes

NOTE See Guide To Using JSON for detailed information on how to work with JSON in GameMaker.

语法:

json_decode(string)

参数类型描述
stringStringThe JSON formatted string to decode

 

返回:

DS Map or -1 (if it fails)

 

例子:

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);
    while (is_string(curr))
    {
        global.Name[n] = ds_map_find_value(map, "name");
        curr = ds_map_find_next(map, curr);
    }
}
ds_map_destroy(resultMap);

上面的代码将解码一个 JSON 字符串并解析它以生成一个全局数组。