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 Json value from an api and display it in a GUI
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 6 Posters 775 Views 2 Watching
  • 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.
  • M Offline
    M Offline
    MehdiMak96
    wrote on last edited by MehdiMak96
    #1

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

    Hello,

    I'm trying to right a code that can do the following things:

    1.Take what ever i have chosen in my Combobox as (Variable).
    2. But the (Variable) in my Json code that can read a value from an api.
    3. show me the value in a text browser

    Here is my code:

    void Corona_Aktuell::on_comboBox_currentIndexChanged(const QString &arg1)
    {
        QString name=ui->comboBox->currentText();
        {
          QStringList ArgsList = QApplication::arguments();
         // 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.corona-zahlen.org/districts"));
          QNetworkReply* reply = m_manager.get(request);
          // connect to signal  when its done using lambda)
          QObject::connect(reply, &QNetworkReply::finished, [reply, this]() {
            // 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("+name+"));
            qDebug() << " value is" << value.toString();;
            reply->deleteLater(); // make sure to clean up
    
           this->ui->textBrowser->setText(value);
    
    
          });
    
        }
    
    }
    

    I'm getting this error :
    use of undeclared identifier 'Value'

    But i already defined 'Value'

    Thank you

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      I'm getting this error :
      use of undeclared identifier 'Value'

      Where exactly do you get this error? I don't see that you use a variable named 'Value' anywhere in your example code.

      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
      1
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi and welcome to devnet,

        Beside the point made by @Christian-Ehrlicher, your code will not work as your QNetworkAccessManager is local to the function and will thus be destroyed before the query completes.

        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
        0
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by
          #4

          Further to the points of @Christian-Ehrlicher and @SGaist, it is worth noting that QNetworkReply::readAll() returns a QByteArray and QJsonDocument::fromJson() expects a QByteArray but you are going via an intermediate QString. This imposes unneeded transforms.

          Oddly, the code comment indicates you know about the scope issue @SGaist raised.

          1 Reply Last reply
          2
          • Christian EhrlicherC Christian Ehrlicher

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

            I'm getting this error :
            use of undeclared identifier 'Value'

            Where exactly do you get this error? I don't see that you use a variable named 'Value' anywhere in your example code.

            M Offline
            M Offline
            MehdiMak96
            wrote on last edited by MehdiMak96
            #5

            @Christian-Ehrlicher

            in this line this line

            this->ui->textBrowser->setText(value)
            

            @SGaist

            so what should i do to avoid this problem

            @ChrisW67

            Can you please be more specific and help me go step by step to right this code "solve this problem"

            i just started learning Qt and i don't know what to do

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

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

              in this line this line
              this->ui->textBrowser->setText(value)

              This can't be since there is no Value parameter anywhere in this line.

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

              1 Reply Last reply
              2
              • M MehdiMak96

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

                Hello,

                I'm trying to right a code that can do the following things:

                1.Take what ever i have chosen in my Combobox as (Variable).
                2. But the (Variable) in my Json code that can read a value from an api.
                3. show me the value in a text browser

                Here is my code:

                void Corona_Aktuell::on_comboBox_currentIndexChanged(const QString &arg1)
                {
                    QString name=ui->comboBox->currentText();
                    {
                      QStringList ArgsList = QApplication::arguments();
                     // 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.corona-zahlen.org/districts"));
                      QNetworkReply* reply = m_manager.get(request);
                      // connect to signal  when its done using lambda)
                      QObject::connect(reply, &QNetworkReply::finished, [reply, this]() {
                        // 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("+name+"));
                        qDebug() << " value is" << value.toString();;
                        reply->deleteLater(); // make sure to clean up
                
                       this->ui->textBrowser->setText(value);
                
                
                      });
                
                    }
                
                }
                

                I'm getting this error :
                use of undeclared identifier 'Value'

                But i already defined 'Value'

                Thank you

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by JonB
                #7

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

                use of undeclared identifier 'Value'

                • Please copy & paste the error message you receive.
                • The error message will contain a line number. Please find that exact line number in your code, and copy & paste that line too.

                Your code contains the following:

                     // if in mainwindow. make sure m_manager lives in .h as member. else it wont work. ( death by scope)
                      QNetworkAccessManager m_manager;
                

                Someone has written that comment to tell you that you need to move the declaration QNetworkAccessManager m_manager;, which is currently local to the function, out to a scope where it persists longer than that function, till the reply has finished. For example, perhaps it could be a member variable in your class Corona_Aktuell.

                M 1 Reply Last reply
                3
                • mrjjM Offline
                  mrjjM Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on last edited by mrjj
                  #8

                  Hi
                  @MehdiMak96
                  the value variable is in small letters
                  QJsonValue value = obj.value(QString("+name+"));
                  but error shows as captital first.
                  use of undeclared identifier 'Value'
                  Are they written the same ?

                  Is the task, to read the data associated with the QString &arg1

                  {
                     "data":{
                        "10041":{
                           "ags":"10041",
                           "name":"Regionalverband Saarbrücken",
                           "county":"LK Stadtverband Saarbrücken",
                           "state":"Saarland",
                           "population":328714,
                           "cases":15789,
                           "deaths":466,
                           "casesPerWeek":24,
                           "deathsPerWeek":0,
                           "stateAbbreviation":"SL",
                           "recovered":15257,
                           "weekIncidence":7.301179748961103,
                  

                  Should you not then read the data file once, instead of each time the user chooses something in the Combobox ?

                  do you use the "10041" id to look it up or how should it work ?

                  1 Reply Last reply
                  0
                  • JonBJ JonB

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

                    use of undeclared identifier 'Value'

                    • Please copy & paste the error message you receive.
                    • The error message will contain a line number. Please find that exact line number in your code, and copy & paste that line too.

                    Your code contains the following:

                         // if in mainwindow. make sure m_manager lives in .h as member. else it wont work. ( death by scope)
                          QNetworkAccessManager m_manager;
                    

                    Someone has written that comment to tell you that you need to move the declaration QNetworkAccessManager m_manager;, which is currently local to the function, out to a scope where it persists longer than that function, till the reply has finished. For example, perhaps it could be a member variable in your class Corona_Aktuell.

                    M Offline
                    M Offline
                    MehdiMak96
                    wrote on last edited by MehdiMak96
                    #9

                    @Christian-Ehrlicher @JonB

                    In this line I'm Getting the error

                    his->ui->textBrowser->setText(value);
                    

                    the error:

                    error: no viable conversion from 'QJsonValue' to 'const QString'

                    @mrjj

                    The whole idea is to let the user to choose whatever city in the combobox and show him the "weekIncidence" in that city in a text browser .
                    I just figured another problem to right a code that can only read the "weekIncidence" :/

                    JonBJ 2 Replies Last reply
                    0
                    • M MehdiMak96

                      @Christian-Ehrlicher @JonB

                      In this line I'm Getting the error

                      his->ui->textBrowser->setText(value);
                      

                      the error:

                      error: no viable conversion from 'QJsonValue' to 'const QString'

                      @mrjj

                      The whole idea is to let the user to choose whatever city in the combobox and show him the "weekIncidence" in that city in a text browser .
                      I just figured another problem to right a code that can only read the "weekIncidence" :/

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

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

                      error: no viable conversion from 'QJsonValue' to 'const QString'

                      But you wrote earlier that there error message was:

                      use of undeclared identifier 'Value'

                      ? And got everybody to look for that....

                      1 Reply Last reply
                      1
                      • M MehdiMak96

                        @Christian-Ehrlicher @JonB

                        In this line I'm Getting the error

                        his->ui->textBrowser->setText(value);
                        

                        the error:

                        error: no viable conversion from 'QJsonValue' to 'const QString'

                        @mrjj

                        The whole idea is to let the user to choose whatever city in the combobox and show him the "weekIncidence" in that city in a text browser .
                        I just figured another problem to right a code that can only read the "weekIncidence" :/

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by JonB
                        #11

                        @MehdiMak96
                        Anyway....

                        QJsonValue value = obj.value(QString("+name+"));
                        this->ui->textBrowser->setText(value);
                        
                        error: no viable conversion from 'QJsonValue' to 'const QString'
                        

                        setText(), requires a string. value is type QJsonValue. You already know that's not a string and how to convert it because you have

                        qDebug() << " value is" << value.toString();;
                        
                        1 Reply Last reply
                        3
                        • M Offline
                          M Offline
                          MehdiMak96
                          wrote on last edited by MehdiMak96
                          #12

                          @JonB
                          yeah I'm sorry that is because my line was out side the bracket in my computer

                          like this :

                                });
                            this->ui->textBrowser->setText(value);
                              }
                          
                          }
                          
                          JonBJ 1 Reply Last reply
                          0
                          • M MehdiMak96

                            @JonB
                            yeah I'm sorry that is because my line was out side the bracket in my computer

                            like this :

                                  });
                              this->ui->textBrowser->setText(value);
                                }
                            
                            }
                            
                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @MehdiMak96
                            OK, but that's not in the code you showed. And you didn't say anything when you discovered it so we knew.

                            Anyway for that new error your post crossed with mine, see my previous post above.

                            1 Reply Last reply
                            0
                            • M Offline
                              M Offline
                              MehdiMak96
                              wrote on last edited by
                              #14

                              Thanks the error is gone but it showed nothing on the text browser

                              JonBJ 1 Reply Last reply
                              0
                              • M MehdiMak96

                                Thanks the error is gone but it showed nothing on the text browser

                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by
                                #15

                                @MehdiMak96
                                That's down to you. Only you know what is the JSON you are reading. You might put more debugging output in your code, or run it in debugger.

                                Also if you have not acted on what has been said about the scope of the QNetworkAccessManager m_manager; you may have problems. But you'll know that from your debugging.

                                M 1 Reply Last reply
                                1
                                • JonBJ JonB

                                  @MehdiMak96
                                  That's down to you. Only you know what is the JSON you are reading. You might put more debugging output in your code, or run it in debugger.

                                  Also if you have not acted on what has been said about the scope of the QNetworkAccessManager m_manager; you may have problems. But you'll know that from your debugging.

                                  M Offline
                                  M Offline
                                  MehdiMak96
                                  wrote on last edited by
                                  #16

                                  @JonB but i have no clue what this means

                                      // if in mainwindow. make sure m_manager lives in .h as member. else it wont work. ( death by scope)
                                        QNetworkAccessManager m_manager;
                                  

                                  can you please explain it to me

                                  mrjjM JonBJ 2 Replies Last reply
                                  0
                                  • M MehdiMak96

                                    @JonB but i have no clue what this means

                                        // if in mainwindow. make sure m_manager lives in .h as member. else it wont work. ( death by scope)
                                          QNetworkAccessManager m_manager;
                                    

                                    can you please explain it to me

                                    mrjjM Offline
                                    mrjjM Offline
                                    mrjj
                                    Lifetime Qt Champion
                                    wrote on last edited by mrjj
                                    #17

                                    @MehdiMak96

                                    Hi
                                    I wrote that comment.
                                    It means
                                    QNetworkAccessManager m_manager; here is a local variable.
                                    Those are deleted as soon a function ends.
                                    QNetworkAccessManager is asynchronous which means the m_manager might be deleted before
                                    the QNetworkReply::finished is fired.

                                    So its better to move it to part of Corona_Aktuell class.
                                    That is to put it in the .h inside the class. as a class member.

                                    Also regarding your json parsing.
                                    Do note that the data holding class (the cites) is inside an object called
                                    Data. so the current code might be in the wrong json class and hence return nothing
                                    for "name" as that one level deeper.

                                    Is this school task ? Im asking to know how much we can help without breaking the rules.

                                    1 Reply Last reply
                                    3
                                    • M MehdiMak96

                                      @JonB but i have no clue what this means

                                          // if in mainwindow. make sure m_manager lives in .h as member. else it wont work. ( death by scope)
                                            QNetworkAccessManager m_manager;
                                      

                                      can you please explain it to me

                                      JonBJ Offline
                                      JonBJ Offline
                                      JonB
                                      wrote on last edited by JonB
                                      #18

                                      @MehdiMak96
                                      QNetworkAccessManager m_manager has to "persist"/"be in existence"/"not go out of scope" until after the slot you attached to the reply's finished signal has completed.

                                      In an earlier post I made a suggestion as to what you could do about it.

                                      1 Reply Last reply
                                      3
                                      • M Offline
                                        M Offline
                                        MehdiMak96
                                        wrote on last edited by
                                        #19
                                        This post is deleted!
                                        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