Creating a json::value from a json string

Jun 6, 2013 at 5:55 PM
I have a string
"{
"username": "xyz",
"password": "abc"
}"
How can I convert it to json::value? I found a way wherein I create a http_response with content-type "application/json" and body as the above string and then use the function extract_json. But surely there has to be a simpler way?
Jun 6, 2013 at 10:05 PM
How about using value parse static method:

parse link
Jun 7, 2013 at 3:40 AM
Edited Jun 7, 2013 at 3:40 AM
I tried using it though I was unsuccessful. The above string is in std::string format so I converted it to istringstream for the parse method but it did not work for me!
Jun 7, 2013 at 4:06 AM
parse() is definitely the way to go.

Please take a look at our section on platform-independent strings, and take a look in the source for our unit tests, there are numerous examples of parsing strings there.

There are conversion methods between different kinds of strings in the utility::conversions namespace.

Please note, also, that when you are starting with string literals, you have to escape the '"' characters in the input string.

Niklas
Jun 7, 2013 at 6:00 PM
I am escaping the "" characters at the start and the end. Also I tried using the conversion example.
json::value x;
x.parse(utility::conversions::to_string_t(MY_JSON));
where MY_JSON is the above std::string but I still can't recreate the json::value. It fails to iterate.
Jun 7, 2013 at 6:04 PM
OK,

Please provide a larger snippet of code that you are trying to compile and we'll take a look at what is going wrong.

Niklas
Jun 7, 2013 at 6:11 PM
Edited Jun 7, 2013 at 6:13 PM
Here is my code snippet:
 std:string MY_JSON = "{
     "username": "xyz",
     "password": "abc"
     }";
 json::value x;
 x.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
    }
This is what I am trying to do, but it doesn't enter the for loop whereas if I do something like this to create my json_value it enters the loop
    http_response r;
    r.set_body(MY_JSON, L"application/json");
    json::value x = r.extract_json().get();

Thanks,
Jaimin
Jun 7, 2013 at 6:24 PM
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
    }
Jun 7, 2013 at 6:44 PM
Thanks I got it working!