I am trying read a text file which has Valid JSON content but not string. The below code works fine if it is a string dump. For example - if file contents are like this "{ \"happy\": true, \"pi\": 3.141 }"
then it will parse without errors. Now I want to find out a way which minimizes these conversion ? How to convert JSON content to String dump in C++ using any standard lib? I am using nlohmann
for now, but seems like this requires additional coding. Please educate me if I can hack this with simple code.
My Code
#include <iostream>
#include <fstream>
#include <streambuf>
#include <nlohmann/json.hpp>
using namespace std;
using json = nlohmann::json;
int main()
{
std::fstream f_json("C://json.txt");
json jFile;
try {
jFile = json::parse(f_json);
}
catch (json::parse_error &e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}
Our client produces JSON files which is like below.
{
"happy": true,
"pi": 3.141
}