代码之家  ›  专栏  ›  技术社区  ›  S Andrew

无法在C++中写入JSON数据到文件

  •  0
  • S Andrew  · 技术社区  · 7 年前

    我有一个JSON数据,我想在C++中写入JSON文件。我在用 nlohmann json 下面是代码:

    using nlohmann::json;
    
    std::ofstream output_file("C:\\Program Files (x86)\\output.json");
    
    json outJson;
    
    std::time_t now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
    std::string created(30, '\0');
    std::strftime(&created[0], created.size(), "%Y-%m-%d %H:%M:%S", std::localtime(&now));
    
    outJson["Created"] = created;
    outJson["DataId"] = "T-452";
    outJson["Type"] = "UserData";
    
    output_file << outJson;
    

    但没有任何东西能被拯救 output.json .

    0 回复  |  直到 7 年前
        1
  •  0
  •   RandomPerfectHashFunction    7 年前

    显式序列化和调用 close() 在离开之前,我们应该在这里做这个把戏。

    output_file << outJson.dump(4);
    output_file.close();