将 cpprestsdk json 值对象写入文件
Write a cpprestsdk json value object into a file
我收到一个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:%un", 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
文件中,但不确定如何完成它。写入ostream
对象并打开someValue
serialize.c_cstr()
会产生奇怪的输出。
ostream o("someFile.json");
o << setw(4) << someValue.serialize().c_str() << std<<endl;
如果你的系统 typdef std::string 是一个宽字符串,那么通过 ostream 输出会导致奇怪的输出。
web::json::value::serialize()
返回一个utility::string_t
https://microsoft.github.io/cpprestsdk/classweb_1_1json_1_1value.html#a5bbd4bca7b13cd912ffe76af825b0c6c
utility::string_t
是typedef作为std::string
https://microsoft.github.io/cpprestsdk/namespaceutility.html#typedef-members
所以这样的事情会起作用:
std::filesystem::path filePath(L"c:\someFile.json"); // Assuming wide chars here. Could be U instead of L depending on your setup
std::wofstream outputFile(filePath);
outputFile << someJSONValue.serialize().c_str();
outputFile.close();
相关文章:
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何在h文件中包含.o对象文件
- 如何使用单独文件中的派生类访问友元函数对象
- QML:修改在不同QML文件(而非main.QML)中定义的子对象的属性
- 使用逗号解析文件对象?
- 使用 CIN COUT 显示垃圾值的文件对象?
- 如何确保我的"ifstream"文件对象指向的文件内容已更新?
- 如何从 GDI+ 图元文件对象获取原始图元文件字节
- pyQt5 将文件对象传递给 QWebEngineView
- 从文件对象的当前位置删除字符
- 使用相同的文件对象按顺序写入和读取文件
- 将 ETW 文件对象与 DiskIO 事件相关联
- 如何在构建时将 JSON 文件/对象包含在C++中
- 如何使用运算符<<通过 << 将文件对象馈送到我的类?
- 确定两个文件路径引用同一文件对象的最佳方法是什么
- 文件映射对象和文件对象可以互换使用吗
- 如何判断给定点是否在STereoLithography(.stl)文件对象c++内
- 如何使用Windows Portable Devices C++API获取MTP设备公开的文件夹中的所有文件(对象)的列
- 如何使用结构传递文件对象
- 文件对象->文件名不返回文件的完整路径