且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C ++:使用nlohmann json从文件中读取json对象

更新时间:2023-01-15 23:10:36

2017年3月3日更新 适用于现代C ++版本3的JSON

版本3.0 起,不推荐使用json::json(std::ifstream&).人们应该使用

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);


JSON for Modern C ++ version 2的更新

版本2.0 起,


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);


现代C ++版本1的 JSON的原始答案

使用json::operator>>(std::istream&):


Original answer for JSON for Modern C++ version 1

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

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