简体   繁体   English

如何让我的价格向量 = 我的 json 数组中的值

[英]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.

相关问题 如何使该数组填充我的输入值? - How can I get this array to populate with my Input Values? 如何在 class 中添加对向量? - How can I add vector of pairs inside my class? 如何使用我的函数填充向量? - How can I populate a vector with my functions? 如何使动态数组或向量以与标准数组类似的速度运行? C ++ - How can I make my dynamic array or vector operate at a similar speed to a standard array? C++ 如何在构造函数中将对象数组初始化为相同的值? - How can I initialize an array of objects to the same values in my constructor? 我无法让程序正确读取输入文件中的值(2D数组) - I can't get my program to read the values from my input file correctly (2D array) 如何将数组的值获取到向量然后返回到另一个数组? - How do I get the values of an array to a vector then back to another array? 如何使我的 class 在 Visual Studio 中像 std::array 和 std::vector 一样调试友好? - How can I make my class as debug friendly as std::array and std::vector in Visual Studio? 如何让我的地址显示我的数组中的值? - How do I make my adress show the values in my array? 从 json 对象/文件中,如何导出属于数组中键的值,同时将它们作为字符串存储在向量中? - From a json object/file, how can I derive the values belonging to keys within an array while storing them as a string within a vector?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM