serialize_to_file.cpp
#include <boost/json.hpp>
#include <fstream>
#include <iostream>
namespace json = boost::json;
int main() {
// JSON-Objekt erstellen
json::object obj{
{"name", "John Doe"},
{"age", 30},
{"city", "New York"}
};
// Ausgabe-Dateistream öffnen
std::ofstream file("output.json");
if (file.is_open()) {
// Serialize the JSON object and write it to the file
file << obj;
// Dateistream schließen
file.close();
std::cout << "JSON object serialized and written to file successfully." << std::endl;
} else {
std::cout << "Unable to open file for writing." << std::endl;
}
return 0;
}Kompilieren mit:
build_boostjson.sh
g++ -o main main.cpp -lboost_jsonNach dem Ausführen des Programms mit ./main wird output.json so aussehen:
output.json
{"name":"John Doe","age":30,"city":"New York"}
Ähnliche Beiträge nach Kategorie:
C/C++
Wenn dieser Beitrag dir geholfen hat, erwäge, mir einen Kaffee zu spendieren oder per PayPal zu spenden, um die Recherche und Veröffentlichung neuer Beiträge auf TechOverflow zu unterstützen