Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 599
BRabbit27 Avatar asked Apr 17 '15 22:04

BRabbit27


People also ask

How can I convert JSON to string?

Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);

How do you pass a JSON object into a string in Java?

JSONObject json= (JSONObject) JSONValue. parse(jsonData); JSONObject data = (JSONObject) json. get("data"); After you have parsed the json data, you need to access the data object later get "map" data to json string.

Which method converts a JSON string?

The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.


1 Answers

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); 
like image 137
Jordan Doyle Avatar answered Sep 25 '22 21:09

Jordan Doyle