AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Solved Parse json encoded by PHP (https://forums.alliedmods.net/showthread.php?t=343507)

Dragos 07-31-2023 14:51

Parse json encoded by PHP
 
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;
}


fysiks 08-01-2023 01:20

Re: Parse json encoded by PHP
 
  1. I don't see any JSON parsing going on in your AMX Mod X code and you never mentioned what you tried or showed your code (how can we help you with your JSON code if you don't post your JSON code?).
  2. More importantly, why would you be trying to waste significant processing time on the game server to parse JSON when you can properly store this data in the database (since it's well suited for a table itself) and just read it like you would with a normal query?

lexzor 08-01-2023 05:14

Re: Parse json encoded by PHP
 
if you have any problem with amxmodx json library, post here


in your situation, first option will be to do what fysiks said

second one would be to create a socket server on web side from which retrieve what data you need

Dragos 08-01-2023 05:29

Re: Parse json encoded by PHP
 
Good morning,

I studied more in the library and get what I wanted.

What I really needed was to parse from one json to another json and gather its information.

The json: players_tree is uploaded only once into a very big variable and then disappears like magic.

from this:

Code:


{"1":{"id":3,"nickname":"AMGShowtime","photo":"https:\/\/avatars.steamstatic.com\/55a6e8f6a6e0eb9b26ed37312fd5e67f8b9f4ca8_full.jpg"},"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}}

I parsed every first json object, 1.2.3... to 10 and then his value and then I gathered the value from the last object.

this is the code I've used for this operation

Code:


        new
        JSON:object = json_parse(players_tree, false),
        JSON:value
        new count = json_object_get_count(object);
        new key[200]; //stores key name
        new num;
        for (new i = 0; i < count; i++)
        {
                value = json_object_get_value_at(object, i);

                num = json_object_get_number(value, "id", true);
                server_print("ID: %d", num);
                json_object_get_string(value, "nickname", key, charsmax(key), true);
                server_print("Nickname: %s", key);
                json_object_get_string(value, "photo", key, charsmax(key), true);
                server_print("Photo: %s", key);
        }

       
        json_free(value);
        json_free(object);
}

Output:

Code:

ID: 3
Nickname: AMGShowtime
Photo: https://avatars.steamstatic.com/55a6e8f6a6e0eb9b26ed37312fd5e67f8b9f4ca8_full.jpg
ID: 0
Nickname:
Photo:
ID: 0
Nickname:
Photo:
ID: 0
Nickname:
Photo:
ID: 0
Nickname:
Photo:
ID: 0
Nickname:
Photo:
ID: 0
Nickname:
Photo:
ID: 0
Nickname:
Photo:
ID: 0
Nickname:
Photo:
ID: 0
Nickname:
Photo:


Sorry, for the topic, it was night and my brain was dying slowly and slowly. :)

I hope community will find this helpful.

Quote:

Originally Posted by fysiks (Post 2808019)
  1. I don't see any JSON parsing going on in your AMX Mod X code and you never mentioned what you tried or showed your code (how can we help you with your JSON code if you don't post your JSON code?).
  2. More importantly, why would you be trying to waste significant processing time on the game server to parse JSON when you can properly store this data in the database (since it's well suited for a table itself) and just read it like you would with a normal query?

I mean, I don't know any way how I can store all the data, only to create separate columns, like p, 1,2,3... and so on. But I don't know, JSON comes so handy for my page, and now I needed to parse the value and also get the steamid from another json. :))

Upper was just one of the others that will come.

fysiks 08-01-2023 16:03

Re: Parse json encoded by PHP
 
I'm not sure I understood anything in that post.

Dragos 08-02-2023 02:41

Re: Parse json encoded by PHP
 
Quote:

Originally Posted by fysiks (Post 2808049)
I'm not sure I understood anything in that post.

Because the topic it's ended I should not post any more replies, but here it's the explanation, I guess?

For the best view:

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
  }
}

My JSON contains objects and no array (because I like how it looks), and I needed to specify more in amxmodx to parse every object of the parents.

The idea is that I started with the full json and then get the object value data at the first index, then the second, third... to ten

It's like some parents that have friends that are parents who have children with custom values about themselves, like name, age, and hair color. You go to the first parent and ask about its children and the child information (object) is saved in another json handler, and then you start gathering its information.

!! If I was asking just for the parent, I was gathering just the name of their child, not the information.

I'm sure you are just ironic with that, but this will may help someone understand when they will find this topic in 2025 maybe :))?

fysiks 08-03-2023 01:54

Re: Parse json encoded by PHP
 
Quote:

Originally Posted by Dragos (Post 2808069)
I'm sure you are just ironic with that, but this will may help someone understand when they will find this topic in 2025 maybe :))?

I know how JSON works and how you'd need to process it. I was referring to most of your sentences because they didn't really make much sense (I'll give you the benefit of the doubt and assume it's because of a language barrier).


All times are GMT -4. The time now is 17:53.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.