如何从cpprestsdk

时间:2018-03-06 07:03:49

标签: c++ cpprest-sdk

return client.request(methods::GET).then([](http_response response) -> pplx::task<json::value>
{
    if (response.status_code() == status_codes::OK)
    {
        std::cout << "status ok";
        response.headers().set_content_type(L"application/json");

        return response.extract_json();
    }

    // Handle error cases, for now return empty json value... 


    std::cout << "get json data";

    return pplx::task_from_result(json::value());
})
    .then([](pplx::task<json::value> previousTask)
{
    try
    {
        std::cout << "parse json data";

        const json::value& v = previousTask.get();


        utility::string_t jsonval = v.serialize();

        json::value root;

    //  DisplayJSONValue(v);

       wcout << jsonval;
    //   wcout<<jsonval.data.id;
        // Perform actions here to process the JSON value...

        //std::cout << v;

        //IterateJSONValue();


    }
    catch (const http_exception& e)
    {
        // Print error.
        wostringstream ss;
        ss << e.what() << endl;
        wcout << ss.str();
    }
});

}

请帮助我。提前谢谢。我得到了api回复。但是我无法解析回复。我能够打电话并接受api回复。但是无法解析回复。 任何解析json数据的步骤,请帮助我。

MY API回复

{“offset”:0,“rows”:[{“id”:“1f960b4aa0a7f4e41868d6d9d513ecf8”,“key”:“ABDALLA ELKADY CPA”,“value”:[“c1d148.aa.ss.com”,“ 53fbbfd1aae9f3b82c89235b8e6bcac28e7a06da1b8aa3ab9164555e543b6f8f “]},{” ID “:” 7ba219914965b79a4a2d9f27d06f1644" , “关键”: “callvox”, “值”:[ “a03096.aa.ss.com”, “e98cdb61030376461cd75108d082b9e8c5f024b068a3e734744b974442e1cd26”]},{ “ID”:“6ac7f0a7c4affee980a4d5b9b95d6b44 ”, “关键”: “cloudgen”, “值”:[ “ce39a9.ss.ss.com”, “f9d507e0ad4b70dbe51d900c2a1aa730eae57d042e805ccf54e153879abe4003”]}

3 个答案:

答案 0 :(得分:4)

try
        {
            std::cout << "parse json data";

            const json::value& v = previousTask.get();




        utility::string_t jsonval = v.serialize();



        //  DisplayJSONValue(v);

         wcout << jsonval;

          auto array = v.at(U("rows")).as_array();


           for (int i = 0; i<array.size(); ++i)
           {
               auto id = array[i].at(U("id")).as_string();
               std::wcout <<"\n"<< id;
               auto key = array[i].at(U("key")).as_string();
               std::wcout <<"\n"<< key;
               auto array2=array[i].at(U("value")).as_array();

               std::wcout << array2[0];
               std::wcout << array2[1];
           }

        }
        catch (const http_exception& e)
        {
            // Print error.
            wostringstream ss;
            ss << e.what() << endl;
            wcout << ss.str();
        }

上面的代码对我有用。我得到了解决方案。

答案 1 :(得分:1)

试一下

auto array = json.at(U("devices")).as_array();
for(int i=0; i<array.size(); ++i)
{
     auto id = array[i].at(U("id")).as_string();
     auto type = array[i].at(U("type")).as_string();
}

答案 2 :(得分:1)

这是怎么回事?

const json::value& v = previousTask.get();
auto a = v[L"rows"].as_array();
for (auto i= a.begin();i!=a.end();i++)
{
auto id = (*i)[L"ID"]
}