I am using the nlohmann's json library to work with json objects in c++. Ultimately, I'd like to read a json object from a file, e.g. a simple object like this.
{ "happy": true, "pi": 3.141 }
I'm not quite sure how to approach this. At https://github.com/nlohmann several ways are given to deserialise from a string literal, however it doesn't seem trivial to extend this to read in a file. Does anyone have experience with this?
Since version 3.0, json::json(std::ifstream&)
is deprecated. One should use json::parse()
instead:
std::ifstream ifs("test.json"); json jf = json::parse(ifs); std::string str(R"({"json": "beta"})"); json js = json::parse(str);
For more basic information on how to use nlohmann's json library, see nlohmann FAQ.
Since version 2.0, json::operator>>() id deprecated
. One should use json::json()
instead:
std::ifstream ifs("{\"json\": true}"); json j(ifs);
Use json::operator>>(std::istream&)
:
json j; std::stringstream ifs("{\"json\": true}"); ifs >> j;
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