C++: Reading a json object from file with nlohmann json

混江龙づ霸主 提交于 2019-12-03 05:32:49

Update 2017-07-03 for JSON for Modern C++ version 3

Since version 3.0, json::json(std::ifstream&) is deprecated. One should use json::parse() instead:

std::ifstream ifs("{\"json\": true}");
json j = json::parse(ifs);

Update for JSON for Modern C++ version 2

Since version 2.0, json::operator>>() id deprecated. One should use json::json() instead:

std::ifstream ifs("{\"json\": true}");
json j(ifs);

Original answer for JSON for Modern C++ version 1

Use json::operator>>(std::istream&):

json j;
std::ifstream ifs("{\"json\": true}");
ifs >> j;

The constructor json j(ifs) is deprecated and will be removed in version 3.0.0. Since version 2.0.3 you should write:

std::ifstream ifs("test.json");
json j = json::parse(ifs);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!