My code like this:
std::istringstream file("res/date.json");
std::ostringstream tmp;
tmp<<file.rdbuf();
std::string s = tmp.str();
std::cout<<s<<std::endl;
The output is res/date.json
, while what I really want is the whole content of this json file.
Parsing JSON in C using microjson Developed originally for server-browser communication, the use of JSON has since expanded into a universal data interchange format. This tutorial will provide a simple introduction to parsing JSON strings in the C programming language using the microjson library.
We use the readAllBytes() method of the Files class to read bytes data of the JSON files as a string. We store the returned result of the readAllBytes()and get()methods into a variable and return it to the main() method. In the main() method, we simply print the returned result of the convertFileIntoString()
Load a .json
file into an std::string
and write it to the console:
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
int main(int, char**) {
std::ifstream myFile("res/date.json");
std::ostringstream tmp;
tmp << myFile.rdbuf();
std::string s = tmp.str();
std::cout << s << std::endl;
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With