{
"ADMIN_LIST" :[
{
"name" : "Luke",
"age" : 36,
"id_hash" : "acbfa7acrbad90adb6578adabdff0dcbf80abad43276d79b76f687590390b3ff429"
},
{
"name" : "Sasha",
"age" : 48,
"id_hash" : "97acbfa7acrbad90adb6578adabd0dcbf80abad43276d79b76f687590390b3ff429"
},
{
"name" : "Henry",
"age" : 42,
"id_hash" : "2acbfa7acrbad90adb6578adabd0dcbf80abad493276d79b76f687590390b3ff429"
},
{
"name" : "Jake",
"age" : 31,
"id_hash" : "facbfa7acrbad90adb6578adabd0dcbf80abad432b76d79b76f687590390b3ff429"
},
{
"name" : "Goku",
"age" : 22,
"id_hash" : "0acbfa7acrbad90adb6578adabd0dcbf80abad43276d79b76f687e590390b3ff429"
}
]
}
Having keys named id_hash
within an array named ADMIN_LIST
, I want to get the value of each instance of id_hash
and store it into a string vector std::vector<std::string> Id_Vector = {};
. As simple as that.
The number of Admins varies from json files/objects.... as such a hard-coded predetermined number of key values needed would not be accurate.
file.json
file or a file.txt
containing json formatted text.#include <iostream>
#include "SomeJSON_Library.h"
#include <string>
#include <vector>
int main()
{
std::vector<std::string> Id_Vector = {};
for(int g = 0; j <= Length_Of_Keys; j++) // Where Length_Of_Keys refers to the dynamic number of instances within a json file/object
{
Id_Vector [j] = FromJson.Array.Keys("id_hash");
}
return 0;
}
Such that a call to any id_hash
index would hold relative value gotten from Json File.
#include <iostream>
#include "SomeJSON_Library.h"
#include <string>
#include <vector>
int main()
{
std::vector<std::string> Id_Vector = {};
std::vector<std::string> Admin_Name = {};
for(int j = 0; j <= Length_Of_Keys; j++) // Where Length_Of_Keys refers to the dynamic number of instances within a json file/object
{
Id_Vector [j] = FromJson.Array.Keys("id_hash"); // Get value of key "id_hash"
Admin_Name [j] = FromJson.Array.Keys("name"); // Get value of key "name"
}
// For the sake of confirming implemented code
for(int x = 0; x <= Length_Of_Keys; x++) // Length_Of_Keys or sizeof(Id_Vector[0]) / sizeof(Id_Vector)
{
std::cout << "Id Hash of Admin " << Admin_Name[x] << "is " << Id_Vector[x] << "\n";
}
return 0;
}
Id Hash of Admin Luke is acbfa7acrbad90adb6578adabdff0dcbf80abad43276d79b76f687590390b3ff429
Id Hash of Admin Sasha is 97acbfa7acrbad90adb6578adabd0dcbf80abad43276d79b76f687590390b3ff429
Id Hash of Admin Henry is 2acbfa7acrbad90adb6578adabd0dcbf80abad493276d79b76f687590390b3ff429
Id Hash of Admin Jake is facbfa7acrbad90adb6578adabd0dcbf80abad432b76d79b76f687590390b3ff429
Id Hash of Admin Goku is 0acbfa7acrbad90adb6578adabd0dcbf80abad43276d79b76f687e590390b3ff429
The truth is I'm certain that it's as simple as I've obviously laid it out to be, but I can't for the life of me figure out which Json library or function can do this. I know it's something like FromJsonObj.GetValueFromKey("id_hash");
but I haven't had any luck figuring out how to go about this.
I really wish I knew of a library with such a straight forward syntax call as FromJsonObj.GetValueFromKey();
.
I just need an actual C++ code that implements the illustrated desired result.
Help me out and please don't mark as DUPLICATE....Thanks.
I would use a well-known library, like https://github.com/nlohmann/json.
Here is an example of how you can access array elements by key: accessing elements from nlohmann json