How to get Json value from an api and display it in a GUI
-
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 browserHere 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
-
@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.
-
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.
-
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.
-
in this line this line
this->ui->textBrowser->setText(value)
so what should i do to avoid this problem
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
-
@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. -
@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 classCorona_Aktuell
. -
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 ?
-
In this line I'm Getting the error
his->ui->textBrowser->setText(value);
the error:
error: no viable conversion from 'QJsonValue' to 'const QString'
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" :/ -
@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....
-
@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 typeQJsonValue
. You already know that's not a string and how to convert it because you haveqDebug() << " value is" << value.toString();;
-
@JonB
yeah I'm sorry that is because my line was out side the bracket in my computerlike this :
}); this->ui->textBrowser->setText(value); } }
-
@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.
-
Thanks the error is gone but it showed nothing on the text browser
-
@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. -
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.
-
@MehdiMak96
QNetworkAccessManager m_manager
has to "persist"/"be in existence"/"not go out of scope" until after the slot you attached to the reply'sfinished
signal has completed.In an earlier post I made a suggestion as to what you could do about it.
-
This post is deleted!