Converting a Json::Value to std::string?

BRabbit27 picture BRabbit27 · Apr 18, 2015 · Viewed 47.9k times · Source

I am using JsonCpp to build a JSON object. Once the object is built, is there a way I can get the object as an std::string?

Answer

Jordan Doyle picture Jordan Doyle · Apr 18, 2015

You can use a Json::Writer to do exactly this, since I assume you want to save it somewhere so you don't want human readable output, your best bet would be to use a Json::FastWriter and then you can call the write method with the parameter of your Json::Value (ie. your root) and then that simply returns a std::string like so:

Json::FastWriter fastWriter;
std::string output = fastWriter.write(root);