https://msdn.microsoft.com/library/jj950082.aspx has following code.
void IterateJSONValue()
{
// Create a JSON object.
json::value obj;
obj[L"key1"] = json::value::boolean(false);
obj[L"key2"] = json::value::number(44);
obj[L"key3"] = json::value::number(43.6);
obj[L"key4"] = json::value::string(U("str"));
// Loop over each element in the object.
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;
// Perform actions here to process each string and value in the JSON object...
std::wcout << L"String: " << str.as_string() << L", Value: " << v.to_string() << endl;
}
/* Output:
String: key1, Value: false
String: key2, Value: 44
String: key3, Value: 43.6
String: key4, Value: str
*/
}
However, with C++ REST SDK 2.6.0, it seems that there's no cbegin method in json::value. Without it, what could be the right way to iterate through key:values of a json node (value)?
Looks like the documentation you listed is pegged to version 1.0:
This topic contains information for the C++ REST SDK 1.0 (codename "Casablanca"). If you are using a later version from the Codeplex Casablanca web page, then use the local documentation at http://casablanca.codeplex.com/documentation.
Taking a look at the changelog for version 2.0.0, you'll find this:
Breaking Change - Changed how iteration over json arrays and objects is performed. No longer is an iterator of std::pair<json::value, json::value> returned. Instead there is a separate iterator for arrays and objects on the json::array and json::object class respectively. This allows us to make performance improvements and continue to adjust accordingly. The array iterator returns json::values, and the object iterator now returns std::pair<string_t, json::value>.
I checked the source on 2.6.0 and you're right, there are no iterator methods at all for the value class. It looks like what you'll have to do is grab the internal object
representation from your value
class:
json::value obj;
obj[L"key1"] = json::value::boolean(false);
obj[L"key2"] = json::value::number(44);
obj[L"key3"] = json::value::number(43.6);
obj[L"key4"] = json::value::string(U("str"));
// Note the "as_object()" method calls
for(auto iter = obj.as_object().cbegin(); iter != obj.as_object().cend(); ++iter)
{
// This change lets you get the string straight up from "first"
const utility::string_t &str = iter->first;
const json::value &v = iter->second;
...
}
The most recent documents and versions can be found at GitHub link: https://github.com/microsoft/cpprestsdk
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With