带有restclient cpp的JSON属性树

JSON property trees with restclient-cpp

本文关键字:属性 JSON restclient cpp 带有      更新时间:2023-10-16

我正在尝试使用Boost对JSON数组进行一些计算,我的问题如下:

我如何进行rest调用(使用类似rest客户端cpp的东西)来用JSON数据填充Ptree,而不是使用文件。

以下是我目前所拥有的:

namespace pt = boost::property_tree;
pt::ptree root;
pt::read_json("filename.json", root);

我想用通过GET请求引入的json替换"filename.json"。以下是使用"rest client cpp"时的rest调用示例

RestClient::response r = RestClient::get("http://urlhere.com");

有什么想法或建议吗?

您可以使用接收istream的read_json版本。您应该创建std::istringstream并将响应的内容存储在其中。

RestClient::response r = RestClient::get("http://urlhere.com");
std::istreamstream stream(r.body);
pt::ptree root;
pt::read_json(stream, root);

您可以使用std::istream而不是文件名来调用read_json。