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
?
Use the JavaScript function JSON. stringify() to convert it into a string. const myJSON = JSON. stringify(obj);
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With