Write JSON

    0

    0

    Giovanny Gongora

    Codiga's C++ Recipes

    Use nlohmann lib to write JSON

    #include <iostream>
    #include <nlohmann/json.hpp>
    #include <iomanip>
    #include <vector>
    #include <fstream>
    
    using json = nlohmann::json;
    int readJSON(){
      json i_data;
      std::vector<std::string> ids = i_data["id"];
      std::vector<int> counts = i_data["count"];
      json o_data;
      o_data["id"] = ids;
      o_data["count"] = counts;
      std::ofstream os("o_data.json");
      os << std::setw(4) << o_data << std::endl;
      std::cout << "write complete" << std::endl;
      return 0;
    }
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.