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. When reading QJsonArray it gives always 0
Forum Updated to NodeBB v4.3 + New Features

When reading QJsonArray it gives always 0

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5qjsonarrayreading
25 Posts 7 Posters 4.3k Views 2 Watching
  • 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.
  • sierdzioS Offline
    sierdzioS Offline
    sierdzio
    Moderators
    wrote on last edited by
    #11

    @suslucoder JSON does not convert strings to numbers. So if a value is string and you try to read a double, it will always return 0.

    A workaround for this is:

    qDebug() << "Abc:" << grvArray[i].toObject()["Abc"].toVariant().toInt();
    

    Although it does come with it's problems.

    (Z(:^

    J.HilkJ D 2 Replies Last reply
    2
    • sierdzioS sierdzio

      @suslucoder JSON does not convert strings to numbers. So if a value is string and you try to read a double, it will always return 0.

      A workaround for this is:

      qDebug() << "Abc:" << grvArray[i].toObject()["Abc"].toVariant().toInt();
      

      Although it does come with it's problems.

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #12

      @sierdzio ok, when we start to give etchy solutions 😉

      Local local(QLocale::German);
      qDebug() << "Abc:" << l.toDouble(grvArray[i].toObject()["Abc"].toString());
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      1
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #13

        It's etchiness depends on particular case. In some applications, JSON is used as an internal format, where rules can be different than official ones. And being able to specify a number by either string or number is useful.

        Plus, let's not forget JSON does not support integers at all. So if you want to specify an integer and be sure it won't be fuzzy, using a string is OK.

        (Z(:^

        1 Reply Last reply
        0
        • JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #14

          I don't know where @suslucoder gets his JSON from. And I admit in my code I save/restore integers (C++ type) not floating point. But for integers at least I note that Qt JSON saves as a number, not as a string. So I imagine it would save a floating point similarly. Just saying.

          D 1 Reply Last reply
          0
          • JonBJ JonB

            I don't know where @suslucoder gets his JSON from. And I admit in my code I save/restore integers (C++ type) not floating point. But for integers at least I note that Qt JSON saves as a number, not as a string. So I imagine it would save a floating point similarly. Just saying.

            D Offline
            D Offline
            deleted286
            wrote on last edited by
            #15

            @JonB ı didnt get my json from anywhere. I create it by myself

            JonBJ 1 Reply Last reply
            0
            • sierdzioS sierdzio

              @suslucoder JSON does not convert strings to numbers. So if a value is string and you try to read a double, it will always return 0.

              A workaround for this is:

              qDebug() << "Abc:" << grvArray[i].toObject()["Abc"].toVariant().toInt();
              

              Although it does come with it's problems.

              D Offline
              D Offline
              deleted286
              wrote on last edited by
              #16

              @sierdzio I did it like this way. But it cant read some datas still. What should be the reason? It reads the first and last element of 1 array. And then, didnt read the second item of an array. Read the 4.element of array 2. I didnt understand

              1 Reply Last reply
              0
              • Christian EhrlicherC Online
                Christian EhrlicherC Online
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #17

                Since you created the json manually you're mixing . and ,. Use the proper json notation for doubles without quotes if you want that someone else will be able to read them and don't create such files manually.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                D 1 Reply Last reply
                4
                • Christian EhrlicherC Christian Ehrlicher

                  Since you created the json manually you're mixing . and ,. Use the proper json notation for doubles without quotes if you want that someone else will be able to read them and don't create such files manually.

                  D Offline
                  D Offline
                  deleted286
                  wrote on last edited by
                  #18

                  @Christian-Ehrlicher I have LineEdit. I take datas from line edit on my qui, and save those datas in txt file which is formatted json. I want to take float or double nums on line edit.

                  I have " " because line edit accept strings.

                  Pl45m4P 1 Reply Last reply
                  0
                  • D deleted286

                    @Christian-Ehrlicher I have LineEdit. I take datas from line edit on my qui, and save those datas in txt file which is formatted json. I want to take float or double nums on line edit.

                    I have " " because line edit accept strings.

                    Pl45m4P Offline
                    Pl45m4P Offline
                    Pl45m4
                    wrote on last edited by Pl45m4
                    #19

                    @suslucoder said in When reading QJsonArray it gives always 0:

                    I have " " because line edit accept strings.

                    But you cant save strings in the same way as you save floats / doubles or ints (edit: No ints).

                    @Christian-Ehrlicher said in When reading QJsonArray it gives always 0:

                    It's not 5.0 but "5.0" and this is a string. See also QJsonValue::Type()

                    That's what @Christian-Ehrlicher said here

                    So you have to differentiate what is going to be stored in your Json.
                    (If you build your Json yourself -> strings with "....." and floats with . as separator to avoid read errors)

                    You can save everything as string, but then you have to convert it when reading the values again (-> also works best when using a dot as separator. Not every locale supports , as decimal separator. This is why you probably got 0)


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    1 Reply Last reply
                    2
                    • JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #20

                      @suslucoder
                      ...Which is why I suggested you might choose to save/put in file 41.123 instead of string with quotes like your "41,123", then you will be storing a number and won't have to convert from a string....

                      1 Reply Last reply
                      2
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #21

                        Or use a proper QDoubleSpinbox

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        0
                        • Pl45m4P Offline
                          Pl45m4P Offline
                          Pl45m4
                          wrote on last edited by
                          #22

                          Or a QLineEdit with an input mask, so you can be sure that is input of this widget gonna be a number. Then you can write it as double to your Json.

                          https://doc.qt.io/qt-5/qlineedit.html#inputMask-prop


                          If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                          ~E. W. Dijkstra

                          1 Reply Last reply
                          0
                          • D deleted286

                            @JonB ı didnt get my json from anywhere. I create it by myself

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #23

                            @suslucoder said in When reading QJsonArray it gives always 0:

                            ı didnt get my json from anywhere. I create it by myself

                            So this is not a widget issue, at least at this stage. It's a question of OP deciding on a type to be used in the JSON.

                            1 Reply Last reply
                            0
                            • sierdzioS Offline
                              sierdzioS Offline
                              sierdzio
                              Moderators
                              wrote on last edited by
                              #24

                              Exactly. You should format the data properly when you save it. Then the whole problem will just go away and a plain toDouble() will work when reading the file.

                              (Z(:^

                              D 1 Reply Last reply
                              0
                              • sierdzioS sierdzio

                                Exactly. You should format the data properly when you save it. Then the whole problem will just go away and a plain toDouble() will work when reading the file.

                                D Offline
                                D Offline
                                deleted286
                                wrote on last edited by
                                #25

                                Thanks for all answers. I will reformat my json file and try it again

                                1 Reply Last reply
                                0

                                • Login

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