json_encode


Description

JSON (JavaScript Object Notation) is a lightweight data-interchange format which is easy for to read and write, for both people and machines. It is built on two basic structures:

json_encode takes a ds_map that you have previously created and encoded as a JSON string which you can then use as (for example) part of an http_post_string() call, or so it can be stored externally, written to a file.

NOTE: The hierarchal functionality of JSON is available through special ds_map and ds_list functions, so you are able to encode sub-lists and maps.


Syntax:

json_encode(map)


Argument Description
map a ds_map with the information to encode


Returns:

string


Example:

var hiscore_map, i, str;
hiscore_map = ds_map_create();
for (i = 0; i < 10; i ++;)
   {
   ds_map_add(name[i], score[i]);
   }
str = json_encode(hiscore_map);
get[0] = http_post_string("http://www.angusgames.com/game?game_id=" + string(global.game_id), str)
ds_map_destroy(hiscore_map);

The above code creates a ds_map and then loops through the name and score arrays, adding each key/value pair to the new map. Next, this map is encoded using json_encode and stored as a string in the variable "str". This string is then sent to a web server using http_post_string and the ds_map is destroyed to prevent a memory leak as it is no-longer needed.