C++ Json Boost value to string

Viewed 1263

I have simple C++ code that uses Boost library:

auto jsonStringPtr = jsonValuePtr->if_string();

How can I convert this value to std::string without quotes?

I tested this code:

std::string myString = boost::json::serialize(*jsonStringPtr);

but it contains quotes e.g. "abc" insted of abc... Any idea?

#edit

  • Boost: v1.76.0
  • C++: 20

Example:

boost::json::error_code errorCode;
boost::json::value jsonValue = boost::json::parse("{\"fff\": \"abc\"}", errorCode);

auto jsonString = jsonValue.as_object()["fff"].as_string();
std::string myString = boost::json::serialize(jsonString);
2 Answers

I found the solution:

boost::json::value_to<std::string>(jsonValue)
auto jsonStringPtr = jsonValuePtr->as_string().c_str();
Related