The basic idea is to dynamically retrieve the value of 3 fields in the main function from the Config.json
, namely Id
, Encoding
, Signature
depends on the Id
being passed.
First of all, in the Config.json
file:
Depends on the id, different selection will be applied
e.g. if Id 509 is passed: { "Id": 509, "Encoding": "NO-ENCODING", "Signature": "X509" }
will be applied.
Config.json
{
"SchemaIdForUsage":509,
"Schema":[
{
"Id":100,
"Encoding":"NO-ENCODING",
"Signature":"MD5"
},
{
"Id":509,
"Encoding":"NO-ENCODING",
"Signature":"X509"
}
]
}
I have used below code to parse JSON from the Config.json
:
test.cpp
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int getfile()
{
string line;
ifstream myfile("E:\\config.json");
if (myfile.is_open())
{
while (myfile.good())
{
getline(myfile, line);
cout << line << endl;
}
myfile.close();
}
else cout << "Unable to open file";
system("pause");
return 0;
}
int main()
{
getfile();
return 0;
}
JSON library
https://github.com/mrtazz/restclient-cpp
Hopefully, I've made myself clear as a novice in C++. I am wondering what's a better approach to retrieve those 3 fields value dynamically from the JSON file depends on the Id being passed. Thank you for your help.
Updated Response: using the restclient library to parse JSON
I used below code to try to retrieve JSON
std::ifstream file_input("E:\\test.txt");
Json::Reader reader;
Json::Value root;
reader.parse(file_input, root);
std::cout<<root["Id"];
Json::Value testing = root["Schema"]["Id"];
In the Debugger
I am trying to get the value of the fields Id
eg :100, Encoding
eg "NO-ENCODING"
There's error shows unhandled error
Any idea would be greatly appreciated. Thank you,