Change a QJsonObject into an integer
-
Good day everyone,
I'm making a GUI which contains a spinbox that requests a Jsonvalue from an API when you change the numbers in the spinbox.
I was able (with a lot of help from forum members) the make the request to the API and I'm able to show an updated Json value with every change on the spinbox.
Unfortunally this is not what I have in mind for the final version.
I want to multiply the spinbox with the Json value.
As far as I understand it is not possible since the spinbox value is an integer and the Json value a string.Is there a way to make a Json value into an interger so one can calculate with it?
void MainWindow::on_spinBox_valueChanged(int arg1) { QEventLoop eventLoop; QNetworkAccessManager mgr; QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit())); QNetworkRequest req( QUrl( QString("https://api.bitmart.com/ticker/BMX_ETH") ) ); QNetworkReply *reply = mgr.get(req); eventLoop.exec(); QString html = (QString)reply->readAll(); QJsonDocument jsonResponse = QJsonDocument::fromJson(html.toUtf8()); QJsonObject jsonObj = jsonResponse.object(); //arg2 = jsonObj["bid_1"].toString(); //int n; //n = arg2.toInt(); ui->lineEdit_2->setText(jsonObj["bid_1"].toString()); }
-
@hoonara Does http://doc.qt.io/qt-5/qjsonvalue.html#toInt not work?
-
I have tried it with
QJsonDocument jsonResponse = QJsonDocument::fromJson(html.toUtf8()); QJsonObject jsonObj = jsonResponse.object(); QJsonValue bid1 = jsonObj.value("bid_1"); int n = bid1.toString().toInt();
But if I debug bid1 it returns
Price: QJsonValue(null)and for n
Price: 0 -
@hoonara
can you show an example JSON response? -
I'm afraid I am not that skilled with programming to write error checking.
The code like it is in my inital post gives me the value I expect in the line_edit so I think QJsonDocument works.
Also if i use
qDebug() << JsonObj
I get
QJsonObject({"anchorId": 16,"anchorName": "ETH","ask_1": "0.000280","baseVolume": "2682.243795","bid_1": "0.000275","closeTime": 1.52829e+12,"coinId": 18,"coinName": "BMX","depthEndPrecision": 6,"depthStartPrecision": 4,"high_24h": "0.000320","low_24h": "0.000264","new_24h": "0.000277","openTime": 1.52821e+12,"pair": "BMX_ETH","priceChange": "12.62%","symbolId": 22,"volume": "9045189.820000","website": "https://www.bitmart.com/trade.html?symbol=22"})
If I just use int n = bid1.toInt(); like this
QJsonDocument jsonResponse = QJsonDocument::fromJson(html.toUtf8()); QJsonObject jsonObj = jsonResponse.object(); QJsonValue bid1 = jsonObj.value("bid_1"); int n = bid1.toInt(); qDebug() << n; qDebug() << "Price: " << n;
It says 'class QJsonValue' has no member named 'toInt'. Same for QJsonObject
I thought using
int n = bid1.toString.toInt();
Only turns the 0 infront of .000290 into an integer but I might be wrong because doing the same with "baseVolume" which has "2682.243795" gives 0 as well.
-
@hoonara
Hi
You are asking it to give a float value string back as int.
0.000275 is ZERO as int. int is whole number so that is why.try
QJsonValue bid1 = jsonObj.value("bid_1");
double n = bid1.toDouble();
qDebug() << n; -
@hoonara said in Change a QJsonObject into an integer:
"bid_1": "0.000275"
bid_1 is not a number but a string ... neither toInt() nor toDouble() will work here.
You need to convert it to a string and then you can use QString::toDouble() -
@Christian-Ehrlicher
ahh, you are right. Good catch.
http://doc.qt.io/qt-5/qjsonvalue.html#toDouble
converts the QJsonValue to double if its that type().
in this case it will return defaultValue (0)so its something like
QJsonValue bid1 = jsonObj.value("bid_1"); QString asStr=bid1.toString(); double n = asStr.toDouble(); qDebug() << n;