C++ REST SDK (Casablanca) web::json 迭代

标签 c++ json rest casablanca

https://msdn.microsoft.com/library/jj950082.aspx有以下代码。

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
    */
}

但是,对于 C++ REST SDK 2.6.0,json::value 中似乎没有 cbegin 方法。没有它,迭代 json 节点(值)的键:值的正确方法是什么?

最佳答案

看起来您列出的文档与 1.0 版 Hook :

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.

查看 2.0.0 版的变更日志,您会发现:

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>.

我检查了 2.6.0 的源代码,你是对的,值类根本没有迭代器方法。看起来您需要做的是从您的 value 类中获取内部 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"));

// 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;
    ...
}

最新的文档和版本可以在 GitHub 链接中找到:https://github.com/microsoft/cpprestsdk

关于C++ REST SDK (Casablanca) web::json 迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31674575/

相关文章:

java - 将字符串转换为套接字

java - JSON 对象的 REST Post 不处理 java 中日期值中的空格

c++ - 如何让 IntelliSense 在 Visual Studio 2008 中可靠地工作

c++ - 在 Win32 控制台应用程序中使用 ShutdownBlockRequestCreate

c++ - 未丑化名称的标准库实现源

rest - TFS VSO REST API 工作项(机智)路径给出 404 错误

java - @Recover(后备方法)与 @CircuitBreaker 一起使用时,一旦所有重试都用尽,就不会被调用

c++ - 为什么这个正则表达式与这个字符串不匹配?

xml - 键值对序列化与 JSON、XML 等

java - 对 servlet 的一个请求可以有两种类型的响应