C++11の生文字列リテラル(Raw String Literals)を使うと、簡単なJSONをささっと書けて便利。文字列の中に何も考えずダブルクォートを書ける。
#include <iostream> #include <string> int main() { std::string json = R"({"user_id": 123, "name": "Akira"})"; std::cout << json << std::endl; }
出力:
{"user_id": 123, "name": "Akira"}
Boost.Formatと組み合わせる例:
#include <iostream> #include <string> #include <boost/format.hpp> int main() { std::string json = (boost::format(R"({"user_id": %1%, "name": "%2%"})") % 123 % "Akira" ).str(); std::cout << json << std::endl; }