我正在使用https://github.com/nlohmann/json的库我需要把我的JSON写进文件,但我找不到任何文件,想想看。有人能帮忙吗?
svdrlsy41#
您可以将JSON值j写入文件,如下所示:
j
std::ofstream o("output.json"); o << j << std::endl;
字符串如果希望输出经过美化,请使用:用途:
std::ofstream o("pretty.json"); o << std::setw(4) << j << std::endl;
型
fquxozlt2#
您可以使用dump()方法将json对象转储为字符串,然后将其写入文件。
dump()
zdwk9cvp3#
我今天遇到了这个问题,正如Niels所说,你可以使用shift运算符直接将json写入文件,但这不会用任何空格来保存它,所以它不是很可读。例如,
int main() { nlohmann::json data; data["Some String"] = "Hello World"; data["Some Number"] = 420; data["Empty Array"] = nlohmann::json::array_t(); data["Array With Items"] = { true, "Hello", 3.1415 }; data["Empty Array"] = nlohmann::json::array_t(); data["Object With Items"] = nlohmann::json::object_t({{"Key", "Value"}, {"Day", true}}); data["Empty Object"] = nlohmann::json::object_t(); std::ofstream output_file("settings_example_output.json"); if (!output_file.is_open()) { std::cout << "\n Failed to open output file"; } else { output_file << data; output_file.close(); } return 0; }
字符串生成一个JSON文件,
{"Array With Items":[true,"Hello",3.1415],"Empty Array":[],"Empty Object":{},"Object With Items":{"Day":true,"Key":"Value"},"Some Number":420,"Some String":"Hello World"}
型所以我写了一个export_json函数来插入适当的空格:
template<typename ITERATOR_TYPE> void print_element(ITERATOR_TYPE it, unsigned int depth, std::ostream& output_stream); void print_array(nlohmann::json::array_t arr, unsigned int depth, std::ostream& output_stream); void print_object(nlohmann::json obj, unsigned int depth, std::ostream& output_stream); void export_json(nlohmann::json json_file, std::ostream& output_stream) { output_stream << "{\n"; for (nlohmann::json::iterator iter = json_file.begin();;) { output_stream << '\t' << '"' << iter.key() << "\": "; print_element(iter, 1, output_stream); if (++iter != json_file.end()) { output_stream << ',' << '\n'; } else { break; } } output_stream << "\n}"; } void print_array(nlohmann::json::array_t arr, unsigned int depth, std::ostream& output_stream) { if (!arr.size()) return; std::string indent = std::string(depth, '\t'); for (nlohmann::json::array_t::iterator iter = arr.begin();;) { output_stream << '\n' << indent << '\t'; print_element(iter, depth + 1, output_stream); if (++iter != arr.end()) { output_stream << ','; } else { break; } } output_stream << '\n' << indent; } void print_object(nlohmann::json obj, unsigned int depth, std::ostream& output_stream) { if (!obj.size()) return; std::string indent = std::string(depth, '\t'); for (nlohmann::json::iterator iter = obj.begin();;) { output_stream << '\n' << indent << '\t' << '"' << iter.key() << "\": "; print_element(iter, depth + 1, output_stream); if (++iter != obj.end()) { output_stream << ','; } else { break; } } output_stream << '\n' << indent; } template<typename ITERATOR_TYPE> void print_element(ITERATOR_TYPE iter, unsigned int depth, std::ostream& output_stream) { switch (iter->type()) { case nlohmann::json::value_t::object: output_stream << '{'; print_object(nlohmann::json(*iter), depth, output_stream); output_stream << '}'; break; case nlohmann::json::value_t::array: output_stream << '['; print_array(nlohmann::json::array_t(*iter), depth, output_stream); output_stream << ']'; break; default: output_stream << *iter; break; }
型}当然,如果没有人会阅读文件,那么这是矫枉过正,我会使用前面提到的简单的解决方案。就我个人而言,我想用它来生成设置文件,我可以直接编辑,所以我需要空白。
3条答案
按热度按时间svdrlsy41#
您可以将JSON值
j
写入文件,如下所示:字符串
如果希望输出经过美化,请使用:用途:
型
fquxozlt2#
您可以使用
dump()
方法将json对象转储为字符串,然后将其写入文件。zdwk9cvp3#
我今天遇到了这个问题,正如Niels所说,你可以使用shift运算符直接将json写入文件,但这不会用任何空格来保存它,所以它不是很可读。例如,
字符串
生成一个JSON文件,
型
所以我写了一个export_json函数来插入适当的空格:
型
}
当然,如果没有人会阅读文件,那么这是矫枉过正,我会使用前面提到的简单的解决方案。就我个人而言,我想用它来生成设置文件,我可以直接编辑,所以我需要空白。