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

  • Good day,

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

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

    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("");
        QNetworkRequest request(url);
        //send request
        //read replied data
        //interpret replied data as string/Json

    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.

  • Lifetime Qt Champion

    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.

  • Lifetime Qt Champion

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

    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.


    Since its not a nested it should be very easy.

  • Lifetime Qt Champion

    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;;
     // 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(""));
      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"

  • @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

  • Lifetime Qt Champion

    @Lodhi-bhkr What is your question/problem?

  • @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.

  • Lifetime Qt Champion

    @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: