Problem web::json::value cbegin()

Mar 24, 2014 at 11:10 AM
Hi everybody,

I am new with JSON and with C++ REST SDK.
I found some examples and help on the Internet but I have a strange error:
when I want to iterate a web::json::value with cbegin() visual studio tells me cbegin is not a member of web::json::value.
I checked my headers files, the type, press f1 on the type but everything looks good.
Do you have any solutions ?
void DisplayJSONValue(json::value v)
    if (!v.is_null())
        // Loop over each element in the object
        for (auto iter = v.cbegin(); iter != v.as_object().cend(); ++iter)
            // It is necessary to make sure that you get the value as const reference
            // in order to avoid copying the whole JSON value recursively (too expensive for nested objects)
            const json::value &key = iter->first;
            const json::value &value = iter->second;

            if (value.is_object() || value.is_array())
                // We have an object with children or an array
                if ((!key.is_null()) && (key.is_string()))
                    std::wcout << L"Parent: " << key.as_string() << std::endl;
                // Loop over each element in the object by calling DisplayJSONValue
                if ((!key.is_null()) && (key.is_string()))
                    std::wcout << L"End of Parent: " << key.as_string() << std::endl;
                // Always display the value as a string
                std::wcout << L"Key: " << key.as_string() << L", Value: " << value.to_string() << std::endl;
Mar 24, 2014 at 2:28 PM
Edited Mar 24, 2014 at 2:30 PM
As of 2.0 the value do not return a iterator.

but you are basically missing a "as_object()" and the iter->first return a string_t and not an value.

So if you follow all the examples on the internet you will have to do something like
for (auto iter = v.as_object().cbegin(); iter != v.as_object().cend(); ++iter) {
const string_t& foo = iter->first;
const json::value& bar = iter->second;
if you are sure the json value is an object

If it is an array you do
for (auto iter = v.as_array().cbegin(); iter != v.as_array.cend(); ++iter) {

but use the "testers" to be sure that it is an object or array.
Mar 24, 2014 at 2:53 PM
Ok I don't know why nobody talks about this 2.0 ^^.

Thank you very much and just to say, because maybe will have the same problems, the iterator are not the same.
Thank you again for the answer.
Mar 25, 2014 at 6:49 PM
Hi lolwut,

Yes eskech is correct, these are made in our 2.0.0 release. If you take a look at the release notes under Json this is called out as a breaking change to how iteration is done over json values.

I don't quite understand what you mean by 'nobody talk about this in 2.0'? But let us know if you have any other questions or issues.

Mar 25, 2014 at 7:17 PM

Thank you for your answer.
Work perfectly ^^

Have a good day