-
Notifications
You must be signed in to change notification settings - Fork 67
Serialization (zh)
haibo2.liu edited this page Sep 29, 2022
·
2 revisions
- 序列化为字符串
// 序列化为字符串
std::string json_str = json::dump(j);
// 美化输出,使用 4 个空格对输出进行格式化
std::string pretty_str = json::dump(j, { json::serializer::with_indent(4, ' ') };- 序列化到文件
std::ofstream ofs("output.json");
ofs << j << std::endl;// 将 JSON 内容输出到文件,并美化
std::ofstream ofs("pretty.json");
ofs << std::setw(4) << j << std::endl;- 序列化到输出流
json::value j;
std::cout << json::wrap(j); // 可以使用 std::setw(4) 对输出内容美化- 从字符串中解析
json::value j = json::parse("{ \"happy\": true, \"pi\": 3.141 }");- 从文件中读取
std::ifstream ifs("sample.json");
json::value j;
ifs >> json::wrap(j);- 从用户输入中读取
json::value j;
std::cin >> json::wrap(j);