Senior Member
Join Date: Oct 2018
Location: Romania
|
07-31-2023
, 14:51
Parse json encoded by PHP
|
#1
|
Hey team,
I'm trying to parse the next json that it's encoded in PHP, stored in a MySQL database, and loaded into a amxmodx plugin. The problem is I tried any parse json library i could find (not all for sure), but the best i could find. Do you have any advice or how can i parse the next json?
Code:
{"1":{"id":null,"nickname":null,"photo":null},"2":{"id":null,"nickname":null,"photo":null},"3":{"id":null,"nickname":null,"photo":null},"4":{"id":null,"nickname":null,"photo":null},"5":{"id":null,"nickname":null,"photo":null},"6":{"id":null,"nickname":null,"photo":null},"7":{"id":null,"nickname":null,"photo":null},"8":{"id":null,"nickname":null,"photo":null},"9":{"id":null,"nickname":null,"photo":null},"10":{"id":null,"nickname":null,"photo":null}}
in amxmodx, it only shows when I'm trying to print data gathered from MySQL
Output:
Code:
Avem: 100010 | {"1":{"id":null,"nickname":null,"photo":null},"2":{"id":null,"nickname":null,"photo":null},"3":{"id":null,"nickname":null,"photo":null},"4":{"id":null,"nickname":null,"photo":null},"5":{"id":null,"nickname":null,"photo":null},"6":{"id":nul
MYSQL Code:
Code:
// Global variable
new id[7];
new status[2];
new port[7];
new ip[15];
new players_tree[10204];
// Function Here.....
new error[128];
new g_Error[64];
g_SqlTuple = SQL_MakeDbTuple("localhost","", "", "");
new ErrorCode,Handle:sql = SQL_Connect(g_SqlTuple,ErrorCode,g_Error,charsmax(g_Error));
if(sql == Empty_Handle) set_fail_state(g_Error);
new QueryForm[256];
//formatex(QueryForm, sizeof(QueryForm)-1, "SELECT id, players_tree, status, ip, port FROM `gather_lobby` WHERE ip = %s AND port = %d", g_server_ip, g_server_port);
formatex(QueryForm, sizeof(QueryForm)-1, "SELECT id, players_tree, status, ip, port FROM `gather_lobby`");
new Handle:query;
query = SQL_PrepareQuery(sql, QueryForm);
if (!SQL_Execute(query))
{
SQL_QueryError(query, error, 127)
server_print("Can't execute mysql query | Error %s", error);
} else if (!SQL_NumResults(query)) {
server_print("No result found for this lobby");
} else {
// Exec get
new gcol_id = SQL_FieldNameToNum(query, "id");
new qcol_players_tree = SQL_FieldNameToNum(query, "players_tree");
new qcol_status = SQL_FieldNameToNum(query, "status");
new qcol_ip = SQL_FieldNameToNum(query, "ip");
new qcol_port = SQL_FieldNameToNum(query, "port");
SQL_ReadResult(query, gcol_id, id, sizeof(id) -1);
SQL_ReadResult(query, qcol_players_tree, players_tree, sizeof(players_tree) -1);
SQL_ReadResult(query, qcol_status, status, sizeof(status) -1);
SQL_ReadResult(query, qcol_ip, ip, sizeof(ip) -1);
SQL_ReadResult(query, qcol_port, port, sizeof(port) -1);
server_print("Avem: %s | %s | %s | %s | %s", id, players_tree, status, ip, port)
}
SQL_FreeHandle(query);
SQL_FreeHandle(sql);
Also, this is how I'm encoding in PHP
Code:
function create_players_json() {
$tree = array(
1 => array("id" => null, "nickname" => null, "photo" => null),
2 => array("id" => null, "nickname" => null, "photo" => null),
3 => array("id" => null, "nickname" => null, "photo" => null),
4 => array("id" => null, "nickname" => null, "photo" => null),
5 => array("id" => null, "nickname" => null, "photo" => null),
6 => array("id" => null, "nickname" => null, "photo" => null),
7 => array("id" => null, "nickname" => null, "photo" => null),
8 => array("id" => null, "nickname" => null, "photo" => null),
9 => array("id" => null, "nickname" => null, "photo" => null),
10 => array("id" => null, "nickname" => null, "photo" => null),
);
$myobj = json_encode($tree);
return $myobj;
}
__________________
sup
Last edited by Dragos; 08-03-2023 at 04:07.
|
|