json_parse

This function parses a JSON string and converts it into a collection of nested arrays and structs. An array is the equivalent of a JSON array and a struct is the equivalent of a JSON object.

The JSON should be either previously created using json_stringify or should come from any other valid source.

You supply the string to parse, and the function will return the top-level array or struct which can then be used in your code. If you are not sure of the contents of the JSON, you can use the different Variable Functions (like typeof and struct_get_names in case of a struct) to check the returned contents.

Usage Notes

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

过滤功能 可选

该函数可以选择采用 Function,该函数对结构中的每个值运行一次,包括所有嵌套结构 / 数组及其内部的所有值。

它需要两个参数 (key, value),其中 key 是结构体键名称 (String) 或数组索引 (Real), 是存储在该键 / 索引中的内容。

The filter function must always return a value. It can return a new value, which replaces the key's value in the final converted format returned by json_parse. If no new value should be returned for a particular key, the function must return the original value.

注意 在过滤器函数中覆盖键的值 (使用 return) 时,请确保首先检查其类型,因为过滤器函数也会针对根结构和任何嵌套结构运行,这意味着意外覆盖它们将导致最终结构损坏。请参阅底部的 示例 3

 

语法:

json_parse(json, [filter_func], [inhibit_string_convert])

参数类型描述
jsonString要解析的 JSON 字符串
filter_funcFunction可选 Filter function that processes each item. Don't pass a value or set this to undefined if you only want to set inhibit_string_convert. Syntax: function(key, value) -> new_value
inhibit_string_convertBoolean可选 将此设置为 true 以禁止将字符串转换为运行时引用。默认值:false

 

返回:

Struct or Array

 

示例1:

var json = "{\"myObj\": { \"apples\":10, \"oranges\":12, \"potatoes\":100000, \"avocados\":0 }, \"myArray\":[0, 1, 2, 2, 4, 0, 1, 5, 1]}";

var data = json_parse(json);
show_debug_message(data);

上面的代码创建一个包含有效 JSON 的新字符串,然后调用 json_parse 将该字符串转换为 GML 结构。然后它将结果打印到调试日志中。

NOTE You will notice that the JSON string contains a backslash (\) before every double quote (") inside it:

json = "{ \" myObj

This is to ensure that the double quote is read as an actual character within the string, instead of being read as part of the code and closing the string prematurely. This way we are using a backslash to "escape" the double quote.

If you are loading JSON from an external file however, there is no need to escape characters in that file.


After parsing the JSON string above, if you know its structure, you can use various Variable Functions to check and read its contents:

var data = json_parse(json);

// Check if the struct has myObj variable
if (variable_struct_exists(data, "myObj"))
{
    // Check if it's a struct
    if (is_struct(data.myObj))
    {
        // Print all struct members to the log
        var _names = variable_struct_get_names(data.myObj);
        var _str = "";
        for (var i = 0; i < array_length(_names); i++;)
        {
            _str = _names[i] + ": " + string(variable_struct_get(data.myObj, _names[i]));
            show_debug_message(_str);
        }
    }
}

// Check if the struct has myArray variable
if (variable_struct_exists(data, "myArray"))
{
    // Check if it's an array
    if (is_array(data.myArray))
    {
        show_debug_message(data.myArray);
    }
}

上面的代码将解析给定的 JSON 字符串,生成以下控制台输出:

oranges: 12
potatoes: 100000
avocados: 0
apples: 10
[ 0,1,2,2,4,0,1,5,1 ]

 

示例 2:过滤功能

var json = "{\"myObj\": { \"apples\":10, \"oranges\":12, \"potatoes\":100000, \"avocados\":0 }, \"myArray\":[0, 1, 2, 2, 4, 0, 1, 5, 1]}";

var data = json_parse(json, function (key, value)
{
    show_debug_message($"Key: {key}, Value: {value}");
    return value;
});

上面的代码采用与上一个示例相同的 JSON 字符串,并将其转换为 GML 结构,但是这次它使用了过滤器函数。

过滤器函数将每个项目的键和值打印到输出日志中:

Key: apples, Value: 10
Key: oranges, Value: 12
Key: potatoes, Value: 100000
Key: avocados, Value: 0
Key: myObj, Value: { apples : 10, oranges : 12, potatoes : 100000, avocados : 0 }
Key: 8, Value: 1
Key: 7, Value: 5
Key: 6, Value: 1
Key: 5, Value: 0
Key: 4, Value: 4
Key: 3, Value: 2
Key: 2, Value: 2
Key: 1, Value: 1
Key: 0, Value: 0
Key: myArray, Value: [ 0,1,2,2,4,0,1,5,1 ]
Key: , Value: { myObj : { apples : 10, oranges : 12, potatoes : 100000, avocados : 0 }, myArray : [ 0,1,2,2,4,0,1,5,1 ] }

请注意过滤器函数如何在 JSON 中的结构体 (myObj) 和数组 (myArray) 上运行,以及如何在结构体和数组内的每个项目上运行。它还在根结构上运行,并将键设置为空字符串 (此处,如最后一行所示)。

 

示例 3:覆盖值

var json = "{\"prices\": [2, 5, 1, 2, 4, 5]}";

var data = json_parse(json, function (key, value)
{
    return is_real(value) ? value * 1000 : value;
});

show_debug_message(data);

The above code takes a JSON string containing an array inside a struct. Then json_parse is run with a filter function that multiplies each value with 1000, only when it's a Real. Otherwise it simply returns the value itself.

结果结构如下所示:

{ prices : [ 2000,5000,1000,2000,4000,5000 ] }