serialize_to_file.cpp
#include <boost/json.hpp>
#include <fstream>
#include <iostream>
namespace json = boost::json;
int main() {
// JSON nesnesi oluştur
json::object obj{
{"name", "John Doe"},
{"age", 30},
{"city", "New York"}
};
// Çıktı dosya akışını aç
std::ofstream file("output.json");
if (file.is_open()) {
// JSON nesnesini serileştir ve dosyaya yaz
file << obj;
// Dosya akışını kapat
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;
}Şu komutla derleyin:
build_boostjson.sh
g++ -o main main.cpp -lboost_json./main kullanarak programı çalıştırdıktan sonra, output.json şöyle görünecektir:
output.json
{"name":"John Doe","age":30,"city":"New York"}
Check out similar posts by category:
C/C++
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow