需要将 cURL http 请求的结果存储为 C++ 中的地图
Need to store result of cURL http request as a map in C++
我一直在使用 C++ 中的 cURL 库来发出 HTTP 请求。当将结果存储在字符串中时,它工作得很好,但结果是这样的:
这都是一个连续的字符串。我想访问每个汇率并将它们保存到不同的变量中,以便我可以用它们进行计算。我试过将它保存到 map
而不是 string
,它编译了,但是当 运行 它中止并且不清楚为什么。
给我带来问题的代码在这里:
#define CURL_STATICLIB
#include "curl/curl.h"
#include <iostream>
#include <string>
#include <map>
// change cURL library depending on debug or release config
#ifdef _DEBUG
#pragma comment(lib, "curl/libcurl_a_debug.lib")
#else
#pragma comment (lib, "curl/libcurl_a.lib")
#endif
// extra required libraries
#pragma comment (lib, "Normaliz.lib")
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Wldap32.lib")
#pragma comment (lib, "Crypt32.lib")
#pragma comment (lib, "advapi32.lib")
static size_t my_write(void* buffer, size_t size, size_t nmemb, void* param)
{
std::string& text = *static_cast<std::string*>(param);
size_t totalsize = size * nmemb;
text.append(static_cast<char*>(buffer), totalsize);
return totalsize;
}
int main()
{
// create var to store result from request
std::map<std::string, double> result;
CURL* curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
// determine the setup options for the request
curl_easy_setopt(curl, CURLOPT_URL, "http://api.exchangeratesapi.io/v1/latest?access_key=9c6c5fc6ca81e2411e4058311eafdf3b&symbols=USD,GBP,AUD&format=1");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
// perform the http request
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (CURLE_OK != res) {
std::cerr << "CURL error: " << res << '\n';
}
}
curl_global_cleanup();
std::map <std::string, double> ::const_iterator i;
for (i = result.begin(); i != result.end(); i++)
{
std::cout << i->first << ": " << i->second;
}
}
为了将其保存为字符串,我替换为:
std::map<std::string, double> result;
替换为 std::string result;
并替换:
std::map <std::string, double> ::const_iterator i;
for (i = result.begin(); i != result.end(); i++)
{
std::cout << i->first << ": " << i->second;
}
与 std::cout << result << "\n\n";
.
有了这些替换,它运行良好,但它不是我需要的格式,除非可以从字符串格式中提取特定值?
我觉得我正在尝试做的事情非常具体,我一直在努力寻找可以帮助我的任何在线内容。
我一直在使用 C++ 中的 cURL 库来发出 HTTP 请求。当将结果存储在字符串中时,它工作得很好,但结果是这样的:
这都是一个连续的字符串。我想访问每个汇率并将它们保存到不同的变量中,以便我可以用它们进行计算。我试过将它保存到 map
而不是 string
,它编译了,但是当 运行 它中止并且不清楚为什么。
给我带来问题的代码在这里:
#define CURL_STATICLIB
#include "curl/curl.h"
#include <iostream>
#include <string>
#include <map>
// change cURL library depending on debug or release config
#ifdef _DEBUG
#pragma comment(lib, "curl/libcurl_a_debug.lib")
#else
#pragma comment (lib, "curl/libcurl_a.lib")
#endif
// extra required libraries
#pragma comment (lib, "Normaliz.lib")
#pragma comment (lib, "Ws2_32.lib")
#pragma comment (lib, "Wldap32.lib")
#pragma comment (lib, "Crypt32.lib")
#pragma comment (lib, "advapi32.lib")
static size_t my_write(void* buffer, size_t size, size_t nmemb, void* param)
{
std::string& text = *static_cast<std::string*>(param);
size_t totalsize = size * nmemb;
text.append(static_cast<char*>(buffer), totalsize);
return totalsize;
}
int main()
{
// create var to store result from request
std::map<std::string, double> result;
CURL* curl;
CURLcode res;
curl_global_init(CURL_GLOBAL_DEFAULT);
curl = curl_easy_init();
if (curl) {
// determine the setup options for the request
curl_easy_setopt(curl, CURLOPT_URL, "http://api.exchangeratesapi.io/v1/latest?access_key=9c6c5fc6ca81e2411e4058311eafdf3b&symbols=USD,GBP,AUD&format=1");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, my_write);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &result);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
// perform the http request
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (CURLE_OK != res) {
std::cerr << "CURL error: " << res << '\n';
}
}
curl_global_cleanup();
std::map <std::string, double> ::const_iterator i;
for (i = result.begin(); i != result.end(); i++)
{
std::cout << i->first << ": " << i->second;
}
}
为了将其保存为字符串,我替换为:
std::map<std::string, double> result;
替换为 std::string result;
并替换:
std::map <std::string, double> ::const_iterator i;
for (i = result.begin(); i != result.end(); i++)
{
std::cout << i->first << ": " << i->second;
}
与 std::cout << result << "\n\n";
.
有了这些替换,它运行良好,但它不是我需要的格式,除非可以从字符串格式中提取特定值?
我觉得我正在尝试做的事情非常具体,我一直在努力寻找可以帮助我的任何在线内容。