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(PLUGIN, VERSION, AUTHOR)
readJsonFile()
}
readJsonFile()
{
// Opening your json file
new JSON:jConfigsFile = json_parse("addons/amxmodx/configs/jsonFile.json", true, true)
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 i, JSON:iObjectPos, JSON:jDataList, szCodeString[32];i < json_object_get_count(jConfigsFile);i++)
{
// iObjectPos will hold each object
iObjectPos = json_object_get_value_at(jConfigsFile, i)
json_object_get_string(iObjectPos, "Code", szCodeString, charsmax(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 b, JSON:iDataObjectPos;b < json_array_get_count(jDataList);b++)
{
// iDataObjectPos will hold each object
iDataObjectPos = json_array_get_value(jDataList, b)
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.
__________________