The best way to convert utility::string_t and to_string() to std::string

Jan 13, 2015 at 8:04 AM
Hi there

I'm using Microsoft Cpp REST SDK v1.2 with Visual Studio 2010.
My goal is to read json from web api.

Which is the best way to convert utility::string_t and to_string() to std::string

My code is the following
for(auto iter = obj.cbegin(); iter != obj.cend(); ++iter)
    {
        // Make sure to get the value as const reference otherwise you will end up copying 
        // the whole JSON value recursively which can be expensive if it is a nested object. 
        const json::value &str = iter->first;
        const json::value &v = iter->second;

    std::wcout << L"String: " << str.as_string() << L", Value: " << v.to_string() << std::endl;

    }
Jan 13, 2015 at 5:57 PM
Hi ggiloyan89,

The library contains a set of string conversion functions under the utility::conversions namespace. Specifically for converting from utility::string_t to a UTF-8 std::string you can use to_utf8string(...).

Steve
Marked as answer by ggiloyan89 on 1/15/2015 at 3:11 AM
Jan 15, 2015 at 11:11 AM
Thanks !