QJsonValue decimal number
-
Hi,
I get a response from a web API like:
{"success":true,"message":"some-message","number1":1.234,"number2":"5.678"}
I am using the QDecimal classes to work with decimal numbers instead of float or double. I am able to initialize a decimal number from a QString/QByteArray, but I don't know how to get the 'number1' value from above. This number is not saved as a string in the JSON packet.
Is there any way to get the STRING information from QJson classes? I can try to cut out the needed information with a dirty hack, but I want to use the QJson classes to get this information. If I try to convert the value to double, I don't get the the decimal number but the inaccurate float/double instead.Any idea how to solve this problem?
-
https://stackoverflow.com/questions/45596118/qt-json-decimal-value
?
From:
https://forum.qt.io/topic/23458/solved-qjsonvalue-has-wrong-number-type
sinceQJsonValue
won't deal inQDecimal
s, won't you have to explicitly serialize to/deserialize from string (or maybeQVariant
) if you want to store your original full value into JSON? -
Since I have the reply as QString, there should be a way to get the simple string information at the given JSON key. If I try something like jsonObject.value("number1").toVariant().toString(), the information is internally stored as double and it is impossible to get the original information.
Any hint for an external library for JSON decoding, or should I write a new class which inherits from QJson? I thought there is something like a QByteArray with the base information for exactly this case. How to solve such a problem? There are so many APIs out there which are not using strings for decimal numbers. -
@debegr
You pasted:
{"success":true,"message":"some-message","number1":1.234,"number2":"5.678"}
Correct me if I'm wrong, but you don't havenumber1
's value as a string, you have it as a number. Fornumber2
you have a string. It's what you pasted. -
Right, the value of number1 is in the format number and this is correct, but internal this number is saved in a double and I can't do anything to get a string representation of this value, so that I can use the information in the JSON packet for different decimal operations.
The nearest double from number1 is: 1.2339999999999999857891452847979962825775146484375For better understanding: GOOD APIs always using strings for decimal data (number2), to prevent exactly this problem. If you have a string, it's very easy to parse the information and use it for decimal operations instead of float or double. As I said a few posts above, there are many APIs not using this and therefore there is always this problem if your are using Qt and the standard QJson classes.
Because the problem isn't hard enough, in my case the significant digits of the representing decimal number isn't constant and I can't convert the double value to a string and cut it to the needed size.I hope the problem is clear enough now and anyone out there has an answer for this problem.
Thanks for all your help! -
Hi and welcome to devnet,
One possible way to do that:
QString number1String = number1JsonValue.toVariant().toString()
;` -
@SGaist
Isn't yournumber1JsonValue
already a double, and that's what the problem is? Once JSON has taken it as a double, it's too late. He needs the digits of the number out of the JSON, and I don't think JSON will offer that to him. To me, if he can't control the serialization end he's stuck. But what do I know :) -
Hi @SGaist , no sorry. This was my first try to solve this. It dosn't work because of:
@JNBarchan said:
Once JSON has taken it as a double, it's too late.
With a small test program I testes Qt-JSON: https://github.com/qt-json/qt-json
#include <QCoreApplication> #include <QDebug> #include "json.h" int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QString json = "{\"success\":true,\"message\":\"some-message\",\"number1\":1.234,\"number2\":\"5.678\"}"; bool ok; QtJson::JsonObject result = QtJson::parse(json, ok).toMap(); qDebug() << "number1: " << result["number1"].toString(); qDebug() << "number2: " << result["number2"].toString(); return a.exec(); }
Which gives me the following output:
number1: "1.234" number2: "5.678"
So I think I can work with this library, but a bit sad about the truth, that such a case can't be handled without external libraries.