Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to get application output: Bid value is (2248.18).
Here is code:
int main(int argc, char *argv[])
{
    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());
           auto rootObj = doc.object();
           auto _d = rootObj.value("_d").toArray();
           auto _t = _d[0].toObject().value("_t").toArray();
           auto _quote = _t[0].toObject().value(QString("_quote"));
           qDebug() << _quote;

       reply->deleteLater(); // make sure to clean up
     });
    return a.exec();
}


Now i get:
QJsonValue(string, "2248.18")


What I have tried:

I've tried make:
QJsonObject obj = doc.object();
     qDebug() << obj;
     QJsonValue value = obj.value(QString("_quote"));
     qDebug() << obj.value(QString("_quote"));;
     qDebug() << "Bid value is" << value.toString();;

but i get errors, what should i do?
Posted
Comments
Richard MacCutchan 21-Nov-21 11:08am    
"but i get errors, what should i do?"
Tell us what the errors are, and where they occur.
Oine 73 21-Nov-21 13:07pm    
When i use this code:
QJsonObject obj = doc.object();
qDebug() << obj;
QJsonValue value = obj.value(QString("_quote"));
qDebug() << obj.value(QString("_quote"));;
qDebug() << "Bid value is" << value.toString();;

it makes whole document as an object and then takes value from it but "_quote" isn't in main root of document, so after that in application output it gives: QJsonValue (undefined).

That's why i've made:
auto rootObj = doc.object();
auto _d = rootObj.value("_d").toArray();
auto _t = _d[0].toObject().value("_t").toArray();
auto _quote = _t[0].toObject().value(QString("_quote"));
qDebug() << _quote;

to get value from specified line: obj._d[0]._t[0]._quote of html and it gives
QJsonValue(string, "2248.18")
Now i would like to have output: Bid value is: 2248.18
Do you know what i should do?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900