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.
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.
-
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.
-
@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.
{ "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.
-
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" -
@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.
-
@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