[英]How can I get my price vector to = values inside my json array
Hello I am pretty new to json parsing and parsing in general so I am wondering what is the best way I can assaign the correct values for the price of the underlying stock I am looking at.您好,我对 json 解析和一般解析非常陌生,所以我想知道我可以为我正在查看的基础股票价格分配正确值的最佳方法是什么。 Below is an example of the code I am working with and comments next to them showing kinda what Im confused about
下面是我正在使用的代码示例,它们旁边的注释显示了我对什么感到困惑
Json::Value chartData = IEX::stocks::chart(symbolSearched);
int n = 390;
QVector<double> time(n), price(n);
//Time and Date Setup
QDateTime start = QDateTime(QDate::currentDate());
QDateTime local = QDateTime::currentDateTime();
QDateTime UTC(local);
start.setTimeSpec(Qt::UTC);
double startTime = start.toTime_t();
double binSize = 3600*24;
time[0] = startTime;
price[0] = //First market price of the stock at market open (930AM)
for(int i = 0; i < n; i++)
{
time[i] = startTime + 3600*i;
price[i] = //Stores prices of specific company stock price all the way until 4:30PM(Market close)
}
the charData is the json output with all the data, charData 是带有所有数据的 json output,
.
.
I am wondering how I can get the various values inside the json and store them, and also since its intraday data how can I get it where it doesnt store p[i] if there is no data yet due to it being early in the day, and what is the best way to update this every minute so it continously reads in real time data?我想知道如何获取 json 中的各种值并存储它们,而且由于它的盘中数据,如果由于它是当天早些时候还没有数据,我怎么能得到它不存储 p[i] 的地方,以及每分钟更新一次以连续读取实时数据的最佳方法是什么?
Hope I understood correctly (correct me if not) and you just want to save some subset of json data to your QVector
.希望我理解正确(如果没有,请纠正我)并且您只想将 json 数据的一些子集保存到您的
QVector
中。 Just iterate through all json elements:只需遍历所有 json 元素:
for (int idx = 0; index < chartData.size(); ++idx) {
time[idx] = convert2Timestamp(chartData[idx]["minute"]);
price[idx] = convert2Price(chartData[idx]["high"], chartData[idx]["low"],
chartData[idx]["open"], chartData[idx]["close"], chartData[idx]["average"]);
}
Then you should define what is the logic of convert2Timestamp
(how would you like to store the time information) and the logic of convert2Price
- how would you like to store the price info, only highest/lowest, only the closing value, maybe all of these numbers grouped together in a structure/class.然后你应该定义什么是
convert2Timestamp
的逻辑(你想如何存储时间信息)和convert2Price
的逻辑 - 你想如何存储价格信息,只有最高/最低,只有收盘价,也许所有这些数字组合在一个结构/类中。
Then if you want to execute similar logic every minute to update your locally recorded data, maybe instead of price[idx] = /* something */
you should push additional items that are new to your vector.然后,如果您想每分钟执行一次类似的逻辑来更新本地记录的数据,也许您应该将其他新项目推送到您的向量中,而不是
price[idx] = /* something */
。
If there is a possibility that some of the json keys might not exist, in JsonCPP you could provide a default value eg elem.get(KEY, DEFAULT_VAL)
.如果某些 json 键可能不存在,则在 JsonCPP 中您可以提供默认值,例如
elem.get(KEY, DEFAULT_VAL)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.