http_get and array

P

pacarico

Guest
im currently working in a game where i have to import many names from a DB, i want to store them in an array. or something like that.
how can i make it to work (when i do the request in the DB i use "name" for all the names).
 
P

pacarico

Guest
how can i do it? i tried and got nothing
thanks in advance
 

Nallebeorn

Member
If you have a JSON string like this:
Code:
[Nallebeorn, pacarioco, Donald, Iwata]
You can parse that into a GameMaker ds_list with the following code:
Code:
var map = json_decode(jsonString);
var names = map[?"default"]; // Store the ds_list containing all the names in this variable
/// .... do stuff with the list here ....
ds_map_destroy(map);
json_decode() was designed so that it always returns a ds_map, even when the source JSON only contains an array (i.e. list) -- the map will have a single key "default" which contains the list. Don't forget to destroy map when you don't need it or the names list any more (the list will be destroyed automatically when the parent map is destroyed), or you might end up with a memory leak.

However, I think you need to give us some more info. My first impression was that you retrieved a list of names from an online database (MySQL or something like that), but reading it again I'm not entirely sure if this is the case...?
 
P

pacarico

Guest
If you have a JSON string like this:
Code:
[Nallebeorn, pacarioco, Donald, Iwata]
You can parse that into a GameMaker ds_list with the following code:
Code:
var map = json_decode(jsonString);
var names = map[?"default"]; // Store the ds_list containing all the names in this variable
/// .... do stuff with the list here ....
ds_map_destroy(map);
json_decode() was designed so that it always returns a ds_map, even when the source JSON only contains an array (i.e. list) -- the map will have a single key "default" which contains the list. Don't forget to destroy map when you don't need it or the names list any more (the list will be destroyed automatically when the parent map is destroyed), or you might end up with a memory leak.

However, I think you need to give us some more info. My first impression was that you retrieved a list of names from an online database (MySQL or something like that), but reading it again I'm not entirely sure if this is the case...?


im using this code with the page http://uinames.com/api/?amount=23 and it sends error after show_message(string(json)) it sais its undefined and crash

Code:
var json = async_load[? "result"];
show_message(string(json));

var resultMap = json_decode(json);
show_message(string(resultmap));

var list = ds_map_find_value(resultMap, "default");
show_message(string(list));

var size = ds_list_size(list);
show_message(string(size));

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(map);
   }
ds_list_destroy(list);
ds_map_destroy(resultMap);
 

FrostyCat

Redemption Seeker
Is that piece of code in the HTTP event as it should? Did you check async_load[? 'id'] and async_load[? 'status'] as instructed? Have you paid attention to the Manual entry for http_get(), especially the example at the bottom?
 
P

pacarico

Guest

Nallebeorn

Member
When checking the status, are you checking if it's exactly equal to 0, rather than larger than 0 or not -1? The status might be 1 while the download is in progress but not yet finished. Just an idea.
 
P

pacarico

Guest
When checking the status, are you checking if it's exactly equal to 0, rather than larger than 0 or not -1? The status might be 1 while the download is in progress but not yet finished. Just an idea.
now is workiing but sometimes it cant load the async_load[? "result"] do you know why this happends?

and thanks for all your help :D
 

FrostyCat

Redemption Seeker
now is workiing but sometimes it cant load the async_load[? "result"] do you know why this happends?

and thanks for all your help :D
It can happen when you try to do it with a status of 1 or -1. It can happen when the request failed and you didn't take notice. It can happen when you capture the response of some other request by accident.

In short, this happens when you don't follow the instructions we gave you about checking async_load[? 'id'] and async_load[? 'status'].
 
Top