Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Json Boost value to string

Tags:

c++

json

boost

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);
like image 327
montjet Avatar asked Sep 16 '25 02:09

montjet


2 Answers

I found the solution:

boost::json::value_to<std::string>(jsonValue)
like image 69
montjet Avatar answered Sep 18 '25 19:09

montjet


auto jsonStringPtr = jsonValuePtr->as_string().c_str();
like image 28
Brian Waters Avatar answered Sep 18 '25 19:09

Brian Waters