C ++ REST SDK(Casablanca)web :: json迭代 [英] C++ REST SDK (Casablanca) web::json iteration

查看:609
本文介绍了C ++ REST SDK(Casablanca)web :: json迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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节点(值)的key:值?

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)?

推荐答案

您列出的文档链接到版本1.0:

Looks like the documentation you listed is pegged to version 1.0:


本主题包含C ++ REST SDK 1.0(代号Casablanca)的信息。如果您使用的是Codeplex Casablanca网页的更高版本,请使用 http://casablanca.codeplex上的本地文档.com / documentation

看一下2.0.0版的changelog,你会发现:

Taking a look at the changelog for version 2.0.0, you'll find this:


更改 - 更改了如何对json数组和对象执行迭代。不再是返回的std :: pair的迭代器。相反,在json :: array和json :: object类分别有一​​个单独的迭代器用于数组和对象。这使我们能够提高性能,并继续相应地进行调整。数组迭代器返回json :: values,对象迭代器现在返回std :: pair。

Breaking Change - Changed how iteration over json arrays and objects is performed. No longer is an iterator of std::pair 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.

0,你是对的,没有任何迭代器方法的价值类。看起来你要做的是从类中获取内部对象表示法:

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

这篇关于C ++ REST SDK(Casablanca)web :: json迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆