1

我是 C++ 和 Visual C++ 的新手,正在使用 cpprestsdk aka casablanca 访问 api。

我设法按照它的 github 上的教程进行操作,并能够在终端中显示返回数据。

但我不知道如何显示具体数据。返回数据为json格式。

这是我的代码:

#include "stdafx.h"

#include <iostream>

#include <cpprest/http_client.h>
#include <cpprest/filestream.h>

//using namespace std;

using namespace utility;                    // Common utilities like string conversions
using namespace web;                        // Common features like URIs.
using namespace web::http;                  // Common HTTP functionality
using namespace web::http::client;          // HTTP client features
using namespace concurrency::streams;       // Asynchronous streams

int main(int argc, char* argv[])
{

    auto fileStream = std::make_shared<ostream>();

    // Open stream to output file.
    pplx::task<void> requestTask = fstream::open_ostream(U("results.html")).then([=](ostream outFile)
    {
        *fileStream = outFile;

        // Create http_client to send the request.
        http_client client(U("http://192.168.0.13:3000/api/individual_employment_setting/detail/172"));

        // Build request URI and start the request.
        //uri_builder builder(U("/search"));
        //builder.append_query(U("q"), U("cpprestsdk github"));
        return client.request(methods::GET);
    })

        // Handle response headers arriving.
        .then([=](http_response response)
    {
        printf("Received response status code:%u\n", response.status_code());

        // Write response body into the file.
        // return response.body().read_to_end(fileStream->streambuf());
        stringstreambuf buffer;
        response.body().read_to_end(buffer).get();



        printf("Response body: \n %s", buffer.collection().c_str());

        return  fileStream->print(buffer.collection()); //write to file anyway
    })

        // Close the file stream.
        .then([=](size_t)
    {
        return fileStream->close();
    });

    // Wait for all the outstanding I/O to complete and handle any exceptions
    try
    {
        requestTask.wait();
    }
    catch (const std::exception &e)
    {
        printf("Error exception:%s\n", e.what());
    }

    return 0;
}

api的返回数据:

{
"data":{
    "sch_time_setting":[{
        "holiday":["Saturday","Friday"],
        "biweekly_odd":[],
        "biweekly_even":["Saturday"],
        "clock_in_mon":"08:40",
        "clock_in_tue":"08:40",
        "clock_in_wed":"08:40",
        "clock_in_thu":"08:40",
        "clock_in_fri":"08:40",
        "clock_in_sat":"08:40",
        "clock_in_sun":null,
        "clock_in_hol":null,
        "clock_out_mon":"18:00",
        "clock_out_tue":"18:00",
        "clock_out_wed":"18:00",
        "clock_out_thu":"18:00",
        "clock_out_fri":"18:00",
        "clock_out_sat":"18:00",
        "clock_out_sun":null,
        "clock_out_hol":null,
        "_id":"5a9797b480591678e077118f"
    }],
    "_id":"5a9797b480591678e0771190",
    "staff_id":172,
    "temp_name":"Regular Employment",
    "individual_setting":false,
    "employment_category":"Regular Employee",
    "branch_office":"Cebu Branch Office",
    "availability_status":"Incumbent",
    "req_working_hours":"08:00",
    "fixed_brk_time_from":"12:00",
    "fixed_brk_time_to":"13:00",
    "date_to_start":"2018-03-01T06:03:32.050Z",
    "createdAt":"2018-03-01T06:03:32.066Z",
    "updatedAt":"2018-03-01T06:03:32.066Z",
    "__v":0
    },
"success":true
}
4

1 回答 1

1

定义一个web::json::value变量以从响应中获取 json 响应。

web::json::value response;

现在,一旦请求返回响应并且返回码是OK,那么您可以使用extract_json(). 像这样的东西

.then([&response](http_response _response){
        if (_response.status_code() == web::http::status_codes::OK) {

            response = _response.extract_json().get();
        }
}

现在,您应该可以使用or等json​​对 , 进行操作了。responseresponse.as_object()auto id = response[L"data"][L"_id"].as_string();

于 2018-04-11T05:47:50.733 回答