0

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

enter image description here

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,

epiphany
  • 730
  • 9
  • 25
  • 2
    Don't try to parse JSON yourself, get a library that does it for you. – Some programmer dude Mar 25 '19 at 08:35
  • 1
    Also please read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) What you're doing with `while ( myfile.good() )` is basically the same and just as bad. – Some programmer dude Mar 25 '19 at 08:36
  • @Someprogrammerdude thank you for yr advice, https://github.com/mrtazz/restclient-cpp - the json library I'm using, as the documentation mainly include the usage of HTTP Protocol. May I ask can you share some insight on how to parse JSON using this library – epiphany Mar 25 '19 at 08:46
  • 1
    @epiphany: There's a JSON library i.e. `jsoncpp` in the `vendor` directory (https://github.com/mrtazz/restclient-cpp/tree/master/vendor/jsoncpp-0.10.5/dist). You can check its project (https://github.com/open-source-parsers/jsoncpp) and documentation (http://open-source-parsers.github.io/jsoncpp-docs/doxygen/index.html) to have an idea about how to use it. You can also other options as well e.g. https://github.com/nlohmann/json, https://github.com/Tencent/rapidjson, etc. as well. – Azeem Mar 25 '19 at 09:18
  • @ Azeem : I have used the jsoncpp library to try to get the value of a field, but unsuccessful, any insight? thanks – epiphany Mar 26 '19 at 03:28

0 Answers0