显示api返回数据中的特定数据,C++;卡萨布兰卡休息酒店 我是C++和Visual C++的新手,使用CPPRESTSDK Aka Casabraca访问API。

显示api返回数据中的特定数据,C++;卡萨布兰卡休息酒店 我是C++和Visual C++的新手,使用CPPRESTSDK Aka Casabraca访问API。,c++,visual-c++,casablanca,cpprest-sdk,C++,Visual C++,Casablanca,Cpprest Sdk,我在github上学习了教程,并能够在终端中显示返回数据 但我不知道如何显示特定的数据。返回数据为json格式 这是我的代码: #include "stdafx.h" #include <iostream> #include <cpprest/http_client.h> #include <cpprest/filestream.h> //using namespace std; using namespace utility;

我在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;
}

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

web::json::value response;
现在,一旦请求返回响应并且返回代码为
OK
,那么您就可以使用
extract_json()
从中提取json。像这样的

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

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

现在,您应该能够使用
response.as_object()
auto id=response[L“data”][L“_id”].as_string()对
response
执行
json
操作等等。

您好,先生,我试图将自动id=响应[L“数据”][L“id”]输出为字符串();在控制台中,但它不显示任何数据。
response[L“data”][L“\u id”]。as\u string()
转换为STL
string
。尝试使用
.c_str()
std::wcout进行输出
.then([&response](http_response _response){
        if (_response.status_code() == web::http::status_codes::OK) {

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