Is it possible to convert string_t to string?

Nov 24, 2014 at 3:31 AM
I have been trying unsuccessfully for several hours to figure out how to retrieve a string from within a json object stored within an array that has been retrieved from an api, and then store it in a plain old std::string. I tried simply attempting to assign to a std::string, but that didn't work, as it gave me a type mismatch error (utility::string_t does not match std::string).

I then attempted simply using a utility::string_t object to store the retrieved string, but attempting to use it with cout didn't work, even when using the c_str() conversion function. Using c_str() didn't result in any errors, but the output was junk.

Is there some easy conversion that I'm missing? Should I be trying something different?
Nov 24, 2014 at 5:33 PM
Hi tmengCW

Yes most of the APIs in the C++ Rest SDK operate on the type utility::string_t. This is a platform dependent string type. It is a typedef to a std::wstring containing UTF-16 on Windows and a std::string containing UTF-8 on other platforms. We have an easy to use set of string conversion functions under the utility::conversions namespace that you should take a look at.

If you have a json::value that you know is a string value type then you could retrieve it and convert it to a std::string with something like the following:
std::string str = ::utility::conversions::to_utf8string(myJsonValue.as_string());
Steve