C++ - 如何将 JSON 写回文件

Ver*_*san 2 c++ json c++11

我正在使用https://github.com/nlohmann/json
中的库 ,我需要将 JSON 写入文件,但我在文档中找不到任何关于此的想法。
有人可以帮忙吗?

Nie*_*ann 5

您可以将 JSON 值写入j文件,如下所示:

std::ofstream o("output.json");
o << j << std::endl;
Run Code Online (Sandbox Code Playgroud)

如果您希望美化输出,请使用:

std::ofstream o("pretty.json");
o << std::setw(4) << j << std::endl;
Run Code Online (Sandbox Code Playgroud)