黑客马拉松助手代码
说明: 1)通过执行以下命令进行编译:g ++ main.cpp -ljsoncpp -lcurl 2)运行在工作目录中创建的可执行文件:./a.exe
有用的资源: http://jsoncpp.sourceforge.net/class_json_1_1_value.html
对我来说: Using libCurl and JsonCpp to parse from https webserver
#include <iostream>
#include <string>
using namespace std;
class StockData {
private:
string symbol;
double marketPercent;
int bidSize;
double bidPrice;
int askSize;
double askPrice;
int volume;
double lastSalePrice;
int lastSaleSize;
long lastSaleTime;
long lastUpdated;
string sector;
string securityType;
public:
StockData(string symbol, double marketPercent, int bidSize, double bidPrice, int askSize, double askPrice,
int volume, double lastSalePrice, int lastSaleSize, long lastSaleTime, long lastUpdated, string sector, string securityType) :
symbol(symbol),
marketPercent(marketPercent),
bidSize(bidSize),
bidPrice(bidPrice),
askSize(askSize),
askPrice(askPrice),
volume(volume),
lastSalePrice(lastSalePrice),
lastSaleSize(lastSaleSize),
lastSaleTime(lastSaleTime),
lastUpdated(lastUpdated),
sector(sector),
securityType(securityType) {}
//Read-only getters
string getSymbol() const { return symbol; }
double getMarketPercent() const { return marketPercent; }
int getBidSize() const { return bidSize; }
double getBidPrice() const { return bidPrice; }
int getAskSize() const { return askSize; }
double getAskPrice() const{ return askPrice; }
int getVolume() const { return volume; }
double getLastSalePrice() const { return lastSalePrice; }
int getLastSaleSize() const { return lastSaleSize; }
long getLastSaleTime() const { return lastSaleTime; }
long getLastUpdated() const { return lastUpdated; }
string getSector() const { return sector; }
string getSecurityType() const { return securityType; }
};
#include <cstdint>
#include <iostream>
#include <memory>
#include <string>
#include <curl/curl.h>
#include <json/json.h>
#include <vector>
#include "StockData.cpp"
using namespace std;
class StockDataProvider {
private:
CURL *curl;
CURLcode res;
Json::Value jsonData;
Json::Reader jsonReader;
const string BASE_URL="https://api.iextrading.com/1.0/tops?symbols=";
public:
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
((std::string*)userp)->append((char*)contents, size * nmemb);
return size * nmemb;
}
string getJSON(string url) {
string readBuffer;
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
return readBuffer;
}
return 0;
}
vector<StockData> getStockData(string stockSymbols[]) {
const int size = sizeof(stockSymbols)/sizeof(stockSymbols[0]);
string url = createUrl(stockSymbols, size);
string jsonString = getJSON(url);
/*
* TODO: Implement the jsonParsing using jsoncpp to create the objects
*/
}
/* Use the jsonData to parse through JSON reponse and create StockData objects */
void parseJson(string jsonString) {
if (jsonReader.parse(jsonString, jsonData))
{
cout << "Successfully parsed JSON data" << endl;
cout << "\nJSON data received:" << endl;
cout << jsonData.toStyledString() << endl;
//Need to parse data here!
}
else
{
cout << "Could not parse HTTP data as JSON" << endl;
cout << "HTTP data was:\n" << jsonString << endl;
}
}
string createUrl(string stockSymbols[], int size) {
string url = BASE_URL;
for(int i = 0; i < size; i++) {
url += stockSymbols[i];
if(i < size-1) {
url += ',';
}
}
return url;
}
};
#include "StockDataProvider.cpp"
using namespace std;
int main() {
string URL = "https://api.iextrading.com/1.0/tops?symbols=SNAP,FB";
StockDataProvider stockDataProvider;
string stockSymbols[] = {"SNAP", "FB"};
string newURL = stockDataProvider.createUrl(stockSymbols, 2);
string responseData = stockDataProvider.getJSON(URL);
stockDataProvider.parseJson(responseData);
cout << "Response: " << responseData << endl;
return 0;
}