Raised This Month: $ Target: $400
 0% 

Retrieving data from json format.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Mikka
Member
Join Date: Dec 2018
Old 10-06-2023 , 13:20   Retrieving data from json format.
Reply With Quote #1

Due to the fact that there is no documentation that is understandable enough for me, I am writing to you for help.

Code:
{
    "Code": "ZAQWSX",
    "szData": [
        {
            "Number": 6300,
            "NPrice": 1,
            "BPrice": 0.5
        },
        {
            "Number": 7300,
            "NPrice": 2,
            "BPrice": 1.75
        }
}
After format in amxx:
Code:
new json[] = {"{ "Code": "ZAQWSX", "szData": [ { "Number": 6300, "NPrice": 1, "BPrice": 0.5 }, { "Number": 7300, "NPrice": 2, "BPrice": 1.75 } }"}
How can I retrieve all objects from the szData field?
Mikka is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 10-07-2023 , 00:37   Re: Retrieving data from json format.
Reply With Quote #2

I'm working on something that explains more about how to read/register json files. I had problems getting the informations too but in the end it is easy to work with, had to learn alone using 2-3 plugins as base. I may post a tutorial to help people with that.
-

Basically, you would need loop the json file, and each index, use json_object_get_string to get the column/object name you want to get the information from. When you find it, check if the column/object is an array with json_is_array(), loop through it's size and get again the data inside of it. This cicle will need to be repeated everytime you have another array inside an already existing object/array of objects, loop inside an loop.

Working example with your json file:

PHP Code:
#include <amxmodx>
#include <json>

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "author"

public plugin_init() {
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
readJsonFile()
}

readJsonFile()
{
    
// Opening your json file
    
new JSON:jConfigsFile json_parse("addons/amxmodx/configs/jsonFile.json"truetrue)
    if(
json_is_object(jConfigsFile)) // check if the format you wrote is correct (maybe missing an , or {)
    
{
        
// Looping through each objects inside the json file (the "1" you will see later on)
        
for(new iJSON:iObjectPosJSON:jDataListszCodeString[32];json_object_get_count(jConfigsFile);i++)
        {
            
// iObjectPos will hold each object
            
iObjectPos json_object_get_value_at(jConfigsFilei)
            
            
json_object_get_string(iObjectPos"Code"szCodeStringcharsmax(szCodeString))
            
log_to_file("json_debug.log""Code value: %s"szCodeString)
            
            
// Checking for your array of objects
            
jDataList json_object_get_value(iObjectPos"Data")
            if(
json_is_array(jDataList)) // Checking if the object of arrays is properly written (maybe missing a , or { or ])
            
{
                
// Looping through each objects inside the array
                
for(new bJSON:iDataObjectPos;json_array_get_count(jDataList);b++)
                {
                    
// iDataObjectPos will hold each object
                    
iDataObjectPos json_array_get_value(jDataListb)
                    
                    
log_to_file("json_debug.log""Number value: %d"json_object_get_number(iDataObjectPos"Number"))
                    
log_to_file("json_debug.log""NPrice value: %d"json_object_get_number(iDataObjectPos"NPrice"))
                    
log_to_file("json_debug.log""BPrice value: %.2f"json_object_get_real(iDataObjectPos"BPrice"))
                    
log_to_file("json_debug.log""---------------------------")
                }
            }
            
json_free(jDataList)
        }
    }
    
json_free(jConfigsFile)

Output:

Code:
L 10/07/2023 - 01:38:11: Code value: ZAQWSX
L 10/07/2023 - 01:38:11: Number value: 6300
L 10/07/2023 - 01:38:11: NPrice value: 1
L 10/07/2023 - 01:38:11: BPrice value: 0.50
L 10/07/2023 - 01:38:11: ---------------------------
L 10/07/2023 - 01:38:11: Number value: 7300
L 10/07/2023 - 01:38:11: NPrice value: 2
L 10/07/2023 - 01:38:11: BPrice value: 1.75
L 10/07/2023 - 01:38:11: ---------------------------
Json file:

PHP Code:
{
    
"1":{
          
"Code""ZAQWSX",
          
"Data": [
                      {
                      
"Number"6300,
                      
"NPrice"1,
                      
"BPrice"0.5
                
},
                {
                      
"Number"7300,
                      
"NPrice"2,
                      
"BPrice"1.75
                
}
          ]
    }

If the object is an array, you don't need to index each one like I did with "1" early in the file.
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 10-07-2023 at 18:51.
EFFx is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-07-2023 , 00:48   Re: Retrieving data from json format.
Reply With Quote #3

There are some examples in the original pull request that added this feature: https://github.com/alliedmodders/amxmodx/pull/379. It would be nice if there were more examples.
__________________
fysiks is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 16:04.


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