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. QJsonValue decimal number
Forum Update on Monday, May 27th 2025

QJsonValue decimal number

Scheduled Pinned Locked Moved Unsolved General and Desktop
qjsonvaluejsonqdecimal
8 Posts 3 Posters 4.8k Views
  • 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.
  • D Offline
    D Offline
    debegr
    wrote on 2 Nov 2017, 18:36 last edited by debegr 11 Feb 2017, 18:36
    #1

    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?

    J 1 Reply Last reply 2 Nov 2017, 18:58
    0
    • D debegr
      2 Nov 2017, 18:36

      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?

      J Offline
      J Offline
      JonB
      wrote on 2 Nov 2017, 18:58 last edited by JonB 11 Feb 2017, 19:03
      #2

      @debegr

      https://stackoverflow.com/questions/45596118/qt-json-decimal-value

      ?

      From:
      https://forum.qt.io/topic/23458/solved-qjsonvalue-has-wrong-number-type
      since QJsonValue won't deal in QDecimals, won't you have to explicitly serialize to/deserialize from string (or maybe QVariant) if you want to store your original full value into JSON?

      1 Reply Last reply
      0
      • D Offline
        D Offline
        debegr
        wrote on 2 Nov 2017, 19:19 last edited by
        #3

        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.

        J 1 Reply Last reply 2 Nov 2017, 19:26
        0
        • D debegr
          2 Nov 2017, 19:19

          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.

          J Offline
          J Offline
          JonB
          wrote on 2 Nov 2017, 19:26 last edited by
          #4

          @debegr
          You pasted:
          {"success":true,"message":"some-message","number1":1.234,"number2":"5.678"}
          Correct me if I'm wrong, but you don't have number1's value as a string, you have it as a number. For number2 you have a string. It's what you pasted.

          1 Reply Last reply
          2
          • D Offline
            D Offline
            debegr
            wrote on 2 Nov 2017, 20:30 last edited by
            #5

            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.2339999999999999857891452847979962825775146484375

            For 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!

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 2 Nov 2017, 20:41 last edited by
              #6

              Hi and welcome to devnet,

              One possible way to do that:

              QString number1String = number1JsonValue.toVariant().toString();`

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              J D 2 Replies Last reply 2 Nov 2017, 20:45
              1
              • S SGaist
                2 Nov 2017, 20:41

                Hi and welcome to devnet,

                One possible way to do that:

                QString number1String = number1JsonValue.toVariant().toString();`

                J Offline
                J Offline
                JonB
                wrote on 2 Nov 2017, 20:45 last edited by JonB 11 Feb 2017, 20:46
                #7

                @SGaist
                Isn't your number1JsonValue 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 :)

                1 Reply Last reply
                2
                • S SGaist
                  2 Nov 2017, 20:41

                  Hi and welcome to devnet,

                  One possible way to do that:

                  QString number1String = number1JsonValue.toVariant().toString();`

                  D Offline
                  D Offline
                  debegr
                  wrote on 2 Nov 2017, 21:20 last edited by
                  #8

                  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.

                  1 Reply Last reply
                  0

                  3/8

                  2 Nov 2017, 19:19

                  topic:navigator.unread, 5
                  • Login

                  • Login or register to search.
                  3 out of 8
                  • First post
                    3/8
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • Users
                  • Groups
                  • Search
                  • Get Qt Extensions
                  • Unsolved