Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Change a QJsonObject into an integer
QtWS25 Last Chance

Change a QJsonObject into an integer

Scheduled Pinned Locked Moved Solved General and Desktop
jsonqjsonobjectintegersguichange
10 Posts 6 Posters 5.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    hoonara
    wrote on 6 Jun 2018, 01:47 last edited by
    #1

    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());
    }
    
    J 1 Reply Last reply 6 Jun 2018, 04:26
    0
    • H hoonara
      6 Jun 2018, 01:47

      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());
      }
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 6 Jun 2018, 04:26 last edited by
      #2

      @hoonara Does http://doc.qt.io/qt-5/qjsonvalue.html#toInt not work?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • H Offline
        H Offline
        hoonara
        wrote on 6 Jun 2018, 09:43 last edited by
        #3

        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

        R O J 3 Replies Last reply 6 Jun 2018, 09:59
        0
        • H hoonara
          6 Jun 2018, 09:43

          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

          R Offline
          R Offline
          raven-worx
          Moderators
          wrote on 6 Jun 2018, 09:59 last edited by
          #4

          @hoonara
          can you show an example JSON response?

          --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
          If you have a question please use the forum so others can benefit from the solution in the future

          1 Reply Last reply
          1
          • H hoonara
            6 Jun 2018, 09:43

            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

            O Offline
            O Offline
            oniongarlic
            wrote on 6 Jun 2018, 10:31 last edited by
            #5

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

            1 Reply Last reply
            1
            • H hoonara
              6 Jun 2018, 09:43

              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

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 6 Jun 2018, 11:39 last edited by
              #6

              @hoonara Why not simply

              int n = bid1.toInt();
              

              ?
              And can you show what JSON data you get?

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • H Offline
                H Offline
                hoonara
                wrote on 6 Jun 2018, 13:48 last edited by
                #7

                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.

                M 1 Reply Last reply 6 Jun 2018, 14:52
                0
                • H hoonara
                  6 Jun 2018, 13:48

                  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.

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 6 Jun 2018, 14:52 last edited by mrjj 6 Jun 2018, 14:52
                  #8

                  @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

                  1 Reply Last reply
                  3
                  • C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 6 Jun 2018, 16:41 last edited by
                    #9

                    @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()

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    M 1 Reply Last reply 6 Jun 2018, 16:45
                    4
                    • C Christian Ehrlicher
                      6 Jun 2018, 16:41

                      @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()

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 6 Jun 2018, 16:45 last edited by mrjj 6 Jun 2018, 16:45
                      #10

                      @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;
                      
                      1 Reply Last reply
                      1

                      5/10

                      6 Jun 2018, 10:31

                      • Login

                      • Login or register to search.
                      5 out of 10
                      • First post
                        5/10
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved