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. How to get a Json value from an api and display it in a GUI

How to get a Json value from an api and display it in a GUI

Scheduled Pinned Locked Moved Solved General and Desktop
apijsonqurlqnetworkrequestc++
8 Posts 5 Posters 7.4k 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 4 Jun 2018, 20:11 last edited by
    #1

    Good day,

    I'm tring to make a GUI that processes an api requested value. Those values are in Json format.

    https://api.bitmart.com/ticker/BMX_ETH

    Let's say I want to get the value of "bid_1" and make a pushbutton and a lable that changes into the value of "bid_1" when clicked.

    To make a simple onpushbuttonclicked I use this

    void MainWindow::on_pushButton_clicked()
    {
        ui->label_11->setText("Hello");
    }
    

    Instead of "hello" I want a String with the value of "bid_1".

    As far as I understand I have to use

    void MainWindow::on_pushButton_clicked()
    {
        QUrl url("https://api.bitmart.com/ticker/BMX_ETH");
        QNetworkRequest request(url);
        //send request
        //read replied data
        //interpret replied data as string/Json
        ui->label->setText(QString::number);
    }
    

    But I don't understand and would like to know how to send the request for a specific json value and convert it into a string. I appreciate any help with the given example so I can transfer its solution to my project.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 4 Jun 2018, 20:17 last edited by
      #2

      Hi and welcome to devnet,

      Please take a look at QNetworkAccessManager and QJsonDocument.

      You can load the answer you get for your request in a QJsonDocument and then parse it for what you are interested in and update your UI accordingly.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 4 Jun 2018, 20:57 last edited by
        #3

        @hoonara said in How to get a Json value from an api and display it in a GUI:

        https://api.bitmart.com/ticker/BMX_ETH

        Returns a full json object. Its doubtful it supports asking for one key/value.

        You are supposed to read all of it and parse it and extra the values you want.

        {
          "priceChange":"2.38%",
          "symbolId":22,
          "website":"https://www.bitmart.com/trade.html?symbol=22",
          "depthEndPrecision":6,
          "ask_1":"0.000308",
          "anchorId":16,
          "anchorName":"ETH",
          "pair":"BMX_ETH",
          "volume":"7684655.420000",
          "coinId":18,
          "depthStartPrecision":4,
          "high_24h":"0.000315",
          "low_24h":"0.000290",
          "new_24h":"0.000301",
          "closeTime":1528145084775,
          "bid_1":"0.000294",
          "coinName":"BMX",
          "baseVolume":"2341.535069",
          "openTime":1528058684775
        }
        

        Since its not a nested it should be very easy.

        1 Reply Last reply
        2
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 4 Jun 2018, 21:13 last edited by
          #4

          Hi
          fast sample.
          it need error handling to be useful in real code ! ( dont skip that if for a real program)

          #include <QApplication>
          #include <QDebug>
          #include <QJsonDocument>
          #include <QJsonObject>
          #include <QNetworkReply>
          #include <QNetworkAccessManager>
          
          int main(int argc, char* argv[]) {
            QApplication a(argc, argv);
            MainWindow w;
            w.show();
           // if in mainwindow. make sure m_manager lives in .h as member. else it wont work. ( death by scope)
            QNetworkAccessManager m_manager; 
          // make request 
           QNetworkRequest request = QNetworkRequest(QUrl("https://api.bitmart.com/ticker/BMX_ETH"));
            QNetworkReply* reply = m_manager.get(request);
            // connect to signal  when its done using lambda)
            QObject::connect(reply, &QNetworkReply::finished, [reply]() {
              // read data
              QString ReplyText = reply->readAll();
              // qDebug() << ReplyText;
              // ask doc to parse it
              QJsonDocument doc = QJsonDocument::fromJson(ReplyText.toUtf8());
              // we know first element in file is object, to try to ask for such
              QJsonObject obj = doc.object();
              // ask object for value
              QJsonValue value = obj.value(QString("bid_1"));
              qDebug() << "Bid value is" << value.toString();;
              reply->deleteLater(); // make sure to clean up
            });
            return a.exec();
          }
          

          and it outputs
          Bid value is "0.000290"

          L 1 Reply Last reply 14 Oct 2020, 11:30
          3
          • M mrjj
            4 Jun 2018, 21:13

            Hi
            fast sample.
            it need error handling to be useful in real code ! ( dont skip that if for a real program)

            #include <QApplication>
            #include <QDebug>
            #include <QJsonDocument>
            #include <QJsonObject>
            #include <QNetworkReply>
            #include <QNetworkAccessManager>
            
            int main(int argc, char* argv[]) {
              QApplication a(argc, argv);
              MainWindow w;
              w.show();
             // if in mainwindow. make sure m_manager lives in .h as member. else it wont work. ( death by scope)
              QNetworkAccessManager m_manager; 
            // make request 
             QNetworkRequest request = QNetworkRequest(QUrl("https://api.bitmart.com/ticker/BMX_ETH"));
              QNetworkReply* reply = m_manager.get(request);
              // connect to signal  when its done using lambda)
              QObject::connect(reply, &QNetworkReply::finished, [reply]() {
                // read data
                QString ReplyText = reply->readAll();
                // qDebug() << ReplyText;
                // ask doc to parse it
                QJsonDocument doc = QJsonDocument::fromJson(ReplyText.toUtf8());
                // we know first element in file is object, to try to ask for such
                QJsonObject obj = doc.object();
                // ask object for value
                QJsonValue value = obj.value(QString("bid_1"));
                qDebug() << "Bid value is" << value.toString();;
                reply->deleteLater(); // make sure to clean up
              });
              return a.exec();
            }
            

            and it outputs
            Bid value is "0.000290"

            L Offline
            L Offline
            Lodhi.bhkr
            wrote on 14 Oct 2020, 11:30 last edited by
            #5

            @mrjj : hi i have developed this code to get request reply in qt . But now I need a help to develop another qt application which respond to this get request

            jsulmJ 1 Reply Last reply 14 Oct 2020, 11:33
            0
            • L Lodhi.bhkr
              14 Oct 2020, 11:30

              @mrjj : hi i have developed this code to get request reply in qt . But now I need a help to develop another qt application which respond to this get request

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on 14 Oct 2020, 11:33 last edited by
              #6

              @Lodhi-bhkr What is your question/problem?

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

              L 1 Reply Last reply 31 Oct 2020, 16:45
              1
              • jsulmJ jsulm
                14 Oct 2020, 11:33

                @Lodhi-bhkr What is your question/problem?

                L Offline
                L Offline
                Lodhi.bhkr
                wrote on 31 Oct 2020, 16:45 last edited by
                #7

                @jsulm : I need to develop a rest api like java supports a http request mapping in spring boot which give a json response to the request I send from this code. A Rest API is like when I request to the url with json data , my rest api will respond like another json response according to the request.

                jsulmJ 1 Reply Last reply 2 Nov 2020, 06:24
                0
                • L Lodhi.bhkr
                  31 Oct 2020, 16:45

                  @jsulm : I need to develop a rest api like java supports a http request mapping in spring boot which give a json response to the request I send from this code. A Rest API is like when I request to the url with json data , my rest api will respond like another json response according to the request.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 2 Nov 2020, 06:24 last edited by jsulm 11 Feb 2020, 06:25
                  #8

                  @Lodhi-bhkr Sorry, but this is not a question. What did you try? What problems do you have? Please ask more specific questions.
                  Looks like you asked here already: https://forum.qt.io/topic/120439/qt-code-to-send-data-on-postman-http-post-request

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

                  1 Reply Last reply
                  0

                  • Login

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