尝试从 C++ 中的 cURLlib 获取 JSON 输出
Trying to get a JSON output from cURLlib in c++
所以我在 C++ 中使用 cURLlib,这样我就可以使用 API 获取市场数据,问题是我无法从有关 C++ 的 cURLlib 的文档中得出结论。
API returns 一个 JSON 文件,我想解析它并从中获取数据以用于我自己的算法。
我现在看到的唯一解决方案是解析 cURL 返回的字符串,但我认为这似乎太冗长和俗气,所以如果有某种方式我可以从 cURL 获得直接输出作为 JSON 文件,而不是我可以使用 nlohmann 并以这种方式遍历它。
(我已经更改了提供的 API 密钥,并在代码中将其替换为“演示”)
这是我到目前为止的代码
#include <iostream>
#include <string>
#include <curl/curl.h>
#include<nlohmann/json.hpp>
using namespace std;
using namespace nlohmann;
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp)
{
((string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
void main()
{
string readBuffer;
//we use cURL to obtain the json file as a string
auto curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=1min&apikey=demo");//here demo is the api key
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
vector<string>Entries;
string push;
for (auto it = readBuffer.begin(); it != readBuffer.end(); it++)
{
}
}
因此,如果我有办法获得 JSON 文件作为输出,那将是非常棒的
任何帮助将不胜感激
The only solution I see right now is to parse the string that's returned by cURL
这正是您需要做的。
but I think that seems too lenghty and tacky, so if there's someway I can get a direct output as a JSON file from cURL instead
libcurl 中没有该选项。
I could use nlohmann and iterate through it that way
您已经知道如何从 libcurl 获取字符串形式的 JSON。 nlohmann 解析器可以通过 json::parse()
方法解析一个 JSON 字符串,例如:
std::string readBuffer;
// download readBuffer via libcurl...
json j_complete = nlohmann::json::parse(readBuffer);
// use j_complete as needed...
所以我在 C++ 中使用 cURLlib,这样我就可以使用 API 获取市场数据,问题是我无法从有关 C++ 的 cURLlib 的文档中得出结论。 API returns 一个 JSON 文件,我想解析它并从中获取数据以用于我自己的算法。 我现在看到的唯一解决方案是解析 cURL 返回的字符串,但我认为这似乎太冗长和俗气,所以如果有某种方式我可以从 cURL 获得直接输出作为 JSON 文件,而不是我可以使用 nlohmann 并以这种方式遍历它。
(我已经更改了提供的 API 密钥,并在代码中将其替换为“演示”)
这是我到目前为止的代码
#include <iostream>
#include <string>
#include <curl/curl.h>
#include<nlohmann/json.hpp>
using namespace std;
using namespace nlohmann;
static size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp)
{
((string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
void main()
{
string readBuffer;
//we use cURL to obtain the json file as a string
auto curl = curl_easy_init();
if (curl) {
curl_easy_setopt(curl, CURLOPT_URL, "https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=IBM&interval=1min&apikey=demo");//here demo is the api key
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
curl_easy_perform(curl);
curl_easy_cleanup(curl);
}
vector<string>Entries;
string push;
for (auto it = readBuffer.begin(); it != readBuffer.end(); it++)
{
}
}
因此,如果我有办法获得 JSON 文件作为输出,那将是非常棒的 任何帮助将不胜感激
The only solution I see right now is to parse the string that's returned by cURL
这正是您需要做的。
but I think that seems too lenghty and tacky, so if there's someway I can get a direct output as a JSON file from cURL instead
libcurl 中没有该选项。
I could use nlohmann and iterate through it that way
您已经知道如何从 libcurl 获取字符串形式的 JSON。 nlohmann 解析器可以通过 json::parse()
方法解析一个 JSON 字符串,例如:
std::string readBuffer;
// download readBuffer via libcurl...
json j_complete = nlohmann::json::parse(readBuffer);
// use j_complete as needed...