Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Change a QJsonObject into an integer



  • Good day everyone,

    I'm making a GUI which contains a spinbox that requests a Jsonvalue from an API when you change the numbers in the spinbox.

    I was able (with a lot of help from forum members) the make the request to the API and I'm able to show an updated Json value with every change on the spinbox.

    Unfortunally this is not what I have in mind for the final version.

    I want to multiply the spinbox with the Json value.
    As far as I understand it is not possible since the spinbox value is an integer and the Json value a string.

    Is there a way to make a Json value into an interger so one can calculate with it?

    void MainWindow::on_spinBox_valueChanged(int arg1)
    {
    
        QEventLoop eventLoop;
    
        QNetworkAccessManager mgr;
        QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
    
        QNetworkRequest req( QUrl( QString("https://api.bitmart.com/ticker/BMX_ETH") ) );
        QNetworkReply *reply = mgr.get(req);
        eventLoop.exec();
        QString   html = (QString)reply->readAll();
    
        QJsonDocument jsonResponse = QJsonDocument::fromJson(html.toUtf8());
        QJsonObject jsonObj = jsonResponse.object();
    
        //arg2 = jsonObj["bid_1"].toString();
        //int n;
        //n = arg2.toInt();
    
    
        ui->lineEdit_2->setText(jsonObj["bid_1"].toString());
    }
    

  • Qt Champions 2019



  • I have tried it with

    QJsonDocument jsonResponse = QJsonDocument::fromJson(html.toUtf8());
    QJsonObject jsonObj = jsonResponse.object();
        
    QJsonValue bid1 = jsonObj.value("bid_1");
    int n = bid1.toString().toInt();
    

    But if I debug bid1 it returns
    Price: QJsonValue(null)

    and for n
    Price: 0


  • Moderators

    @hoonara
    can you show an example JSON response?



  • @hoonara First, add more error checking, check that the json to QJsonDocument works and that you get a valid object.


  • Qt Champions 2019

    @hoonara Why not simply

    int n = bid1.toInt();
    

    ?
    And can you show what JSON data you get?



  • I'm afraid I am not that skilled with programming to write error checking.

    The code like it is in my inital post gives me the value I expect in the line_edit so I think QJsonDocument works.

    Also if i use

    qDebug() << JsonObj
    

    I get

    QJsonObject({"anchorId": 16,"anchorName": "ETH","ask_1": "0.000280","baseVolume": "2682.243795","bid_1": "0.000275","closeTime": 1.52829e+12,"coinId": 18,"coinName": "BMX","depthEndPrecision": 6,"depthStartPrecision": 4,"high_24h": "0.000320","low_24h": "0.000264","new_24h": "0.000277","openTime": 1.52821e+12,"pair": "BMX_ETH","priceChange": "12.62%","symbolId": 22,"volume": "9045189.820000","website": "https://www.bitmart.com/trade.html?symbol=22"})
    

    If I just use int n = bid1.toInt(); like this

      QJsonDocument jsonResponse = QJsonDocument::fromJson(html.toUtf8());
      QJsonObject jsonObj = jsonResponse.object();
    
      QJsonValue bid1 = jsonObj.value("bid_1");
      int n = bid1.toInt();
    
      qDebug() << n;
      qDebug() << "Price: " << n;
    

    It says 'class QJsonValue' has no member named 'toInt'. Same for QJsonObject

    I thought using

    int n = bid1.toString.toInt();
    

    Only turns the 0 infront of .000290 into an integer but I might be wrong because doing the same with "baseVolume" which has "2682.243795" gives 0 as well.


  • Lifetime Qt Champion

    @hoonara
    Hi
    You are asking it to give a float value string back as int.
    0.000275 is ZERO as int. int is whole number so that is why.

    try
    QJsonValue bid1 = jsonObj.value("bid_1");
    double n = bid1.toDouble();
    qDebug() << n;

    http://doc.qt.io/qt-5/qjsonvalue.html#toDouble


  • Qt Champions 2019

    @hoonara said in Change a QJsonObject into an integer:

    "bid_1": "0.000275"

    bid_1 is not a number but a string ... neither toInt() nor toDouble() will work here.
    You need to convert it to a string and then you can use QString::toDouble()


  • Lifetime Qt Champion

    @Christian-Ehrlicher
    ahh, you are right. Good catch.
    http://doc.qt.io/qt-5/qjsonvalue.html#toDouble
    converts the QJsonValue to double if its that type().
    in this case it will return defaultValue (0)

    so its something like

    QJsonValue bid1 = jsonObj.value("bid_1");
    QString asStr=bid1.toString();
    double n = asStr.toDouble();
    qDebug() << n;