Home > Software design >  How can I get qjsonvalue to string?
How can I get qjsonvalue to string?

Time:11-23

What should i do to get output for example: Bid value is 2248.48? Here is code:

    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;
eply->deleteLater();

Now i get QJsonValue (string, "2248.48)

when i tried this:

QJsonObject root = _t[0].toObject().value(QString("_quote"));
           qDebug() << root;
QJsonValue value = obj.value(QString("_quote"));
qDebug() << "Bid value is" << value.toString();;

https://api.30.bossa.pl/API/GPW/v2/Q/C/_cat_name/WIG20?_t=1637005413888

{"message":"OK","_quote_date":null,"_type":"C","_symbol":["WIG20"],"_d":[{"_h":"Własne - 22 listopada 2021 16:42","_hs":"Własne","_max_quote_dtm":"22 listopada 2021","_max_quote_dtm_lc":"22 listopada, 16:42","_ret_quote_dtm":"2021-11-22","_t":[{"_symbol":"WIG20","_symbol_short":"WIG20","_group":"X1","_isin":"PL9999999987","_quote_date":"2021.11.22","_quote_time":"16:42","_time":"16:42","_phase":"Sesja","_quote_max":"2262.74","_quote_min":"2237.64","_quote_open":"2251.08","_quote_ref":"2248.18","_quote_imp":"2254.37","_bid_size":null,"_bid_volume":null,"_bid_orders_nr":null,"_ask_size":null,"_ask_volume":null,"_ask_orders_nr":null,"_volume":null,"_open_positions":null,"_quote_volume":null,"_transactions_nr":null,"_turnover_value":841977698,"_quote":"2254.10","_step":"2","_type_of_instrument":"0","_settlement_price":null,"_change_proc":0.26,"_change_pnts":5.9200,"_30d_change_max":2449.6400,"_30d_change_min":2221.6800,"_change_type":"_change_proc","_quote_type":"_quote","_debut":"0","_live":"1","_sw_symbol_short":0,"_is_indice":"1","_change":" 0.26","_change_suffix":"%","_change_max_min":" 1.12","_change_close_open":" 0.13","_change_settl_ref":null}]}],"_i":[null],"_count":1,"_d_fx":{"_h":null,"_hs":null,"_max_quote_dtm":null,"_max_quote_dtm_lc":null,"_t":[]}}

I got erros "QJsonValue to non-scalar type QJsonObject requested"

CodePudding user response:

You either want _quote.toString() (first listing) or root.toString() (second listing)

  • Related