C++ REST SDK (Casablanca) web::json iteration

前端 未结 1 1061
一向
一向 2021-02-19 21:10

https://msdn.microsoft.com/library/jj950082.aspx has following code.

void IterateJSONValue()
{
    // Create a JSON object.
    json::value obj;
    obj[L\"key1\         


        
1条回答
  •  眼角桃花
    2021-02-19 21:28

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

    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

    0 讨论(0)
提交回复
热议问题