【发布时间】:2020-05-16 08:19:07
【问题描述】:
我收到 JSON 响应正文作为 REST 请求的一部分。我想将JSON 的正文写入文件。一个快速的方法是使用 web::http::response 对象本身..
pplx::task<void> requestTask = fstream::open_ostream(U("testResults.json")).then([=](ostream outFile)
{
*fileStream = outFile;
//some work
})
.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());
})
.then([=](size_t s)
{
return fileStream->close();
});
但是,在收到响应正文后,我从中提取 JSON 以计算一些值,之后,如文档所述,无法从 response 正文中提取或再次使用 json,所以我有使用web::json::value 对象。
http::json::value someValue = response.extract_json().get();
我想将此json::value 对象someValue 写入JSON 文件,但不知道如何完成。在someValue 上使用serialize.c_cstr() 写入ostream 对象会产生奇怪的输出。
ostream o("someFile.json");
o << setw(4) << someValue.serialize().c_str() << std<<endl;
【问题讨论】:
标签: c++ json rest cpprest-sdk