serialize_to_file.cpp
#include <boost/json.hpp>
#include <fstream>
#include <iostream>
namespace json = boost::json;
int main() {
// JSON オブジェクトを作成
json::object obj{
{"name", "John Doe"},
{"age", 30},
{"city", "New York"}
};
// 出力ファイルストリームを開く
std::ofstream file("output.json");
if (file.is_open()) {
// JSON オブジェクトをシリアライズしてファイルに書き込む
file << obj;
// ファイルストリームを閉じる
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;
}次のコマンドでコンパイルします:
build_boostjson.sh
g++ -o main main.cpp -lboost_json./main を使用してプログラムを実行すると、output.json は次のようになります:
output.json
{"name":"John Doe","age":30,"city":"New York"}
同じカテゴリの類似記事をチェック:
C/C++
この記事が役に立った場合は、TechOverflow での新しい記事の調査と公開を支援するため、「Buy Me a Coffee」でのご支援、または PayPal 経由でのご寄付をご検討ください