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

nlohmann::json get value as string
nlohmann::json to string
json value to string java
nlohmann/json object to string
json value to string python
nlohmann::json get value as int
nlohmann to_json
json::fastwriter

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?

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);

Converting a Json::Value to std::string?, 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  Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Json::Writer is deprecated and Json::StreamWriterBuilder should instead be used, as stated in the documentation of Json::Writer.

Json::writeString writes into a stringstream and then returns a string:

Json::Value json = ...;
Json::StreamWriterBuilder builder;
builder["indentation"] = ""; // If you want whitespace-less output
const std::string output = Json::writeString(builder, json);

Kudos to cdunn2001's answer here: How to get JsonCPP values as strings?

json-cpp / Discussion / Open Discussion: asString() Exceptions, I'm trying to convert a Json::Value into a std::string. Unfortunately JsonCpp Library throws an std::runtime_error exception which says "Type is not  Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

If your Json::value is of string type, e.g. "bar" in the following json

{
    "foo": "bar"
}

You can use Json::Value.asString to get the value of bar without extra quotes(which will be added if you use StringWriterBuilder). Here is an example:

Json::Value rootJsonValue;
rootJsonValue["foo"] = "bar";
std::string s = rootJsonValue["foo"].asString();
std::cout << s << std::endl; // bar

JSON for Modern C++: nlohmann::basic_json::get, Explicit type conversion between the JSON value and a compatible value which to C++ associative containers such as std::unordered_map<std::string, json> . Then, converting Mat to vector<unsigned char> by using imencode(). Converting vector<unsigned char> to std::string. Encoding std::string by base64 algorithm. Finally, converting encoded std::string to utility::string_t. According to your reply, I replace 3, 4 and 5 three steps with utility::conversions::to_base64(). However the cost time doesn

In my context, I instead used a simple .asString() at the end of my json value object. As @Searene said, it gets rid of the extra quotes that you don't necessary wants if you want to process it after.

Json::Value credentials;
Json::Reader reader;

// Catch the error if wanted for the reader if wanted. 
reader.parse(request.body(), credentials);

std::string usager, password;
usager = credentials["usager"].asString();
password = credentials["password"].asString();

If the value is a int instead of a string, .asInt() works great as well.

Convert a string to json object properly, Some of us ran into problem when it came to converting a std::string or a const char* string properly to json object by nlohmann  Some of us ran into problem when while converting a std::string or a const char* string to json object by nlohmann. Here is my solution .. Tagged with c, json, nlohmann.

Reading and writing JSON in C++, At any rate, our code uses asString to fetch the std::string value encoded as the appropriate implementations to convert standard C++ types to JSON objects. Dismiss Join GitHub today. GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.

Json C++ Examples, create from scratch ---- Json::Value fromScratch; Json::Value array; write in a compact way Json::FastWriter fastWriter; std::string jsonMessage from string ---- // Convert JSON document to string rapidjson::StringBuffer  Is there a way to convert Object to std::string? #579. europelee opened this issue Mar 10, 2016 · 5 comments Labels. question. Comments. Copy link Quote reply

JSON.stringify(), The JSON.stringify() method converts a JavaScript object or value to a JSON string, value: The value to convert to a JSON string. replacer Optional: A Standard data structures JSON.stringify([new Set([1]), new Map([[1, 2]]),  Write into a serde_json::Value using the syntax value[0] = or value["k"] = . If the index is a number, the value must be an array of length bigger than the index. Indexing into a value that is not an array or an array that is too small will panic. If the index is a string, the value must be an object or null which is treated like an

Comments
  • Although this works great, the current version of JsonCPP says Json::FastWriter has been deprecated in favor of Json::StreamWriterBuilder. But the functionality of writing to a string was removed, and now it is necessary to write to a std::stringstream. Can you elaborate on that?