When I make request to API to get bid price, I'm getting QJsonValue undefined, and cannot display it later, what am i doing wrong?
{
QApplication a(argc, argv);
MainWindow w;
w.show();
QNetworkAccessManager m_manager;
// make request
QNetworkRequest request = QNetworkRequest(QUrl("https://api.30.bossa.pl/API/GPW/v2/Q/C/_cat_name/WIG20?_t=1637005413888"));
QNetworkReply* reply = m_manager.get(request);
QObject::connect(reply, &QNetworkReply::finished, [reply]() {
QByteArray rawData = reply->readAll();
QString textData(rawData);
qDebug() << textData;
QJsonDocument doc = QJsonDocument::fromJson(textData.toUtf8());
QJsonObject obj = doc.object();
qDebug() << obj;
QJsonValue value = obj.value(QString("_quote_min"));
qDebug() << obj.value(QString("_quote_min"));;
qDebug() << "Bid value is" << value.toString();;
reply->deleteLater(); // make sure to clean up
});
return a.exec();
}
this is my json:
QJsonObject({"_count":1,"_d":[{"_h":"Własne - 19 listopada 2021 17:15","_hs":"Własne","_max_quote_dtm":"19 listopada 2021","_max_quote_dtm_lc":"19 listopada, 17:15","_ret_quote_dtm":"2021-11-19","_t":[{"_30d_change_max":2453.57,"_30d_change_min":2221.68,"_ask_orders_nr":null,"_ask_size":null,"_ask_volume":null,"_bid_orders_nr":null,"_bid_size":null,"_bid_volume":null,"_change":"-1.02","_change_close_open":"-1.04","_change_max_min":" 2.91","_change_pnts":-23.13,"_change_proc":-1.02,"_change_settl_ref":null,"_change_suffix":"%","_change_type":"_change_proc","_debut":"0","_group":"X1","_is_indice":"1","_isin":"PL9999999987","_live":"0","_open_positions":null,"_phase":"Zamknięcie ostateczne","_quote":"2248.18","_quote_date":"2021.11.19","_quote_imp":"2276.90","_quote_max":"2286.37","_quote_min":"2221.68","_quote_open":"2271.91","_quote_ref":"2271.31","_quote_time":"17:15","_quote_type":"_quote","_quote_volume":null,"_settlement_price":null,"_step":"2","_sw_symbol_short":0,"_symbol":"WIG20","_symbol_short":"WIG20","_time":"17:15","_transactions_nr":null,"_turnover_value":1257698337,"_type_of_instrument":"0","_volume":null}]}],"_d_fx":{"_h":null,"_hs":null,"_max_quote_dtm":null,"_max_quote_dtm_lc":null,"_t":[]},"_i":[null],"_quote_date":null,"_symbol":["WIG20"],"_type":"C","message":"OK"})
CodePudding user response:
If you're confident that the JSON structure will always be the same, then you can find your value like the following. (I broke it down into multiple objects and named them the same way they are named in your JSON file.)
QJsonDocument doc = QJsonDocument::fromJson(textData.toUtf8());
auto rootObj = doc.object();
auto _d = rootObj.value("_d").toArray();
auto _t = _d[0].toObject().value("_t").toArray();
auto _quote_min = _t[0].toObject().value("_quote_min");
qDebug() << _quote_min;
Output:
QJsonValue(string, "2221.68")