Quantcast
Viewing all articles
Browse latest Browse all 4845

New Post: Creating a json::value from a json string

Here's the corrected version:
    std::string MY_JSON = "{ \"username\": \"xyz\", \"password\": \"abc\" }";
    
    json::value x = json::value::parse(utility::conversions::to_string_t(MY_JSON)); 

    for (auto iter = x.cbegin(); iter != x.cend(); ++iter)
    {
        const json::value &key = iter->first;
        const json::value &value = iter->second;
        // Manipulating key and value
    }
Note: parse() is a static function, so its intended use is as a factory, not a manipulator.

BTW, a more efficient way to deal with strings is to use the platform-native string type, utility::string_t. This is std::wstring on Windows and std::string on Linux. The only complication is string literals, for which we have a macro to give the platform-native result. This allows you to avoid unnecessary runtime conversions of strings:
    utility::string_t MY_JSON = U("{ \"username\": \"xyz\", \"password\": \"abc\" }");
    
    json::value x = json::value::parse(MY_JSON); 

    for (auto iter = x.cbegin(); iter != x.cend(); ++iter)
    {
        const json::value &key = iter->first;
        const json::value &value = iter->second;
        // Manipulating key and value
    }

Viewing all articles
Browse latest Browse all 4845

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>