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
QtWS25 Last Chance

When reading QJsonArray it gives always 0

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt5qjsonarrayreading
25 Posts 7 Posters 4.2k 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
    deleted286
    wrote on 12 Feb 2021, 05:43 last edited by
    #1

    I have 5 diferent QJsonArray which depends on single root. I want to read them from a file. I have the code below for it. I have 5,2 in A. But my code returns A = 0 always.

    QJsonObject root = doc.object();
    
    QJsonArray grvArray = root.value("A").toArray();
    for(int i=0; i<grvArray.size(); i++ )
    {
        QJsonObject subArray = grvArray[i].toObject();
        qDebug() << "Abc:" << grvArray[i].toObject()["Abc"].toDouble();
    
    
    }
    
        return a.exec();
    }
    

    Here is my json

    "A": [
        {
            "Abc": "11,48855",
            "Max:": "6,3",
            "Min :": "5,3",
            "Kg": "7,4"
        }
    
    J 1 Reply Last reply 12 Feb 2021, 05:45
    0
    • D deleted286
      12 Feb 2021, 05:43

      I have 5 diferent QJsonArray which depends on single root. I want to read them from a file. I have the code below for it. I have 5,2 in A. But my code returns A = 0 always.

      QJsonObject root = doc.object();
      
      QJsonArray grvArray = root.value("A").toArray();
      for(int i=0; i<grvArray.size(); i++ )
      {
          QJsonObject subArray = grvArray[i].toObject();
          qDebug() << "Abc:" << grvArray[i].toObject()["Abc"].toDouble();
      
      
      }
      
          return a.exec();
      }
      

      Here is my json

      "A": [
          {
              "Abc": "11,48855",
              "Max:": "6,3",
              "Min :": "5,3",
              "Kg": "7,4"
          }
      
      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 12 Feb 2021, 05:45 last edited by
      #2

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

      Here is my json

      If that's your JSON then it is invalid. Please post your real JSON content.

      D 1 Reply Last reply 12 Feb 2021, 05:48
      3
      • J jsulm
        12 Feb 2021, 05:45

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

        Here is my json

        If that's your JSON then it is invalid. Please post your real JSON content.

        D Offline
        D Offline
        deleted286
        wrote on 12 Feb 2021, 05:48 last edited by
        #3

        @jsulm it is to long i wanted to cut them but i forgot some paranthesis.

        Here is

        {
           "Genel":[
              {
                 "Dns Ivme Ktsy:":"41,123",
                 "Dns Msf Ktsy:":"451,52",
                 "Dnste Dumen:":"1,3",
                 "Dnste Gaz:":"6,3",
                 "Dnste Yks r Aci Lmt:":"5.1",
                 "Dnste Yksls:":"78,56",
                 "Gaz Alt Limit:":"454,555",
                 "Gps Bekleme Aci":"9,3",
                 "Ize Yklsm Msf:":"12,36",
                 "Kesisme Aci:":"55.47",
                 "Nkt Kabul:":"52,36",
                 "Telemetri Sklk:":"1215,15",
                 "Yksls r Aci Lmt:":"7,3"
              }
           ],
           "A":[
              {
                 "Abc":"11,48855",
                 "Max:":"6,3",
                 "Min:":"5,3",
                 "Kg":"7,4"
              }
           ],
           "PID":[
              {
                 "Alcalis aci:":"2,3",
                 "G D Kznc":"96,3",
                 "G I Kznc:":"4,36",
                 "G Kmt Lmt:":"74.6",
                 "G P Kznc:":"4,25",
                 "IAS D Kznc:":"21,541",
                 "IAS I Kznc:":"69,3",
                 "IAS P Kznc:":"5,36",
                 "P D Kznc":"45",
                 "P I Kznc:":"335",
                 "P Kmt Kznc:":"254",
                 "P P Kznc:":"22",
                 "R D Kznc:":"6",
                 "R I Kznc:":"5",
                 "R Kmt Lmt:":"2",
                 "R P Kznc:":"4",
                 "Y D Kzncn:":"44,6",
                 "Y I Kzncn:":"2,3",
                 "Y Kmt Lmt:":"7,5",
                 "Y P Kzncn:":"3,6",
                 "Ykls D Kznc:":"7,32",
                 "Ykls I Kznc:":"5,25",
                 "Yksls Aci:":"7,3",
                 "Yksls P Kznc:":"2,36",
                 "Yon Aci:":"9,1",
                 "Yon D Kznc:":"7,5",
                 "Yon I Kznc:":"3,6",
                 "Yon P Kznc:":"45.23"
              }
           ],
           "Servo":[
              {
                 "Alt Nkt:":"7,32",
                 "Motor Yon :":"23,63",
                 "Sag Ana Knt Aci orta:":"5,3",
                 "Sag Ana Knt Alt Nkt:":"3,2",
                 "Sag Ana Knt Orta Nkt:":"7,5",
                 "Sag Ana Knt Ust Nkt:":"9,6",
                 "Sag Ana Knt Yon:":"1,2",
                 "Sag Arka Knt Aci orta:":"47,12",
                 "Sag Arka Knt Alt Nkt:":"23,5",
                 "Sag Arka Knt Orta Nkt:":"78,21",
                 "Sag Arka Knt Ust Nkt:":"12,25",
                 "Sag Arka Knt Yon:":"2,5",
                 "Sol Ana Knt Aci orta:":"7,5",
                 "Sol Ana Knt Alt Nkt:":"7,5",
                 "Sol Ana Knt Orta Nkt:":"1,2",
                 "Sol Ana Knt Ust Nkt:":"9,2",
                 "Sol Ana Knt Yon:":"4,5",
                 "Sol Arka Knt Aci orta:":"",
                 "Sol Arka Knt Alt Nkt:":"",
                 "Sol Arka Knt Orta Nkt:":"",
                 "Sol Arka Knt Ust Nkt:":"",
                 "Sol Arka Knt Yon:":"",
                 "Ust Nkt:":"77,23"
              }
           ],
           "Seyir":[
              {
                 "Mncnk Klks Btrm Sure:":"23,21",
                 "Mncnk Klks Btrm Yksklk:":"4,21",
                 "Motor Kpt Lmt ":"12,36",
                 "Perdo Hiz:":"231,56",
                 "Perdo Sakinma Aci":"12,25",
                 "Pitch Trim:":"2,3",
                 "Prst Rota Zmn Asim:":"7,36",
                 "Roll Trim:":"4,21",
                 "Seyir Hizi IAS:":"23,15",
                 "Seyir Zmn Asim:":"4,12",
                 "Stab Zmn Asim:":"45566",
                 "Yaw Trim:":"4,26"
              }
           ]
        }
        
        Christian EhrlicherC 1 Reply Last reply 12 Feb 2021, 05:56
        0
        • J.HilkJ Offline
          J.HilkJ Offline
          J.Hilk
          Moderators
          wrote on 12 Feb 2021, 05:55 last edited by
          #4

          do not localize your json, stick with the norm and use a dot as a separator for your double/floats


          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.

          D 1 Reply Last reply 12 Feb 2021, 06:11
          5
          • D deleted286
            12 Feb 2021, 05:48

            @jsulm it is to long i wanted to cut them but i forgot some paranthesis.

            Here is

            {
               "Genel":[
                  {
                     "Dns Ivme Ktsy:":"41,123",
                     "Dns Msf Ktsy:":"451,52",
                     "Dnste Dumen:":"1,3",
                     "Dnste Gaz:":"6,3",
                     "Dnste Yks r Aci Lmt:":"5.1",
                     "Dnste Yksls:":"78,56",
                     "Gaz Alt Limit:":"454,555",
                     "Gps Bekleme Aci":"9,3",
                     "Ize Yklsm Msf:":"12,36",
                     "Kesisme Aci:":"55.47",
                     "Nkt Kabul:":"52,36",
                     "Telemetri Sklk:":"1215,15",
                     "Yksls r Aci Lmt:":"7,3"
                  }
               ],
               "A":[
                  {
                     "Abc":"11,48855",
                     "Max:":"6,3",
                     "Min:":"5,3",
                     "Kg":"7,4"
                  }
               ],
               "PID":[
                  {
                     "Alcalis aci:":"2,3",
                     "G D Kznc":"96,3",
                     "G I Kznc:":"4,36",
                     "G Kmt Lmt:":"74.6",
                     "G P Kznc:":"4,25",
                     "IAS D Kznc:":"21,541",
                     "IAS I Kznc:":"69,3",
                     "IAS P Kznc:":"5,36",
                     "P D Kznc":"45",
                     "P I Kznc:":"335",
                     "P Kmt Kznc:":"254",
                     "P P Kznc:":"22",
                     "R D Kznc:":"6",
                     "R I Kznc:":"5",
                     "R Kmt Lmt:":"2",
                     "R P Kznc:":"4",
                     "Y D Kzncn:":"44,6",
                     "Y I Kzncn:":"2,3",
                     "Y Kmt Lmt:":"7,5",
                     "Y P Kzncn:":"3,6",
                     "Ykls D Kznc:":"7,32",
                     "Ykls I Kznc:":"5,25",
                     "Yksls Aci:":"7,3",
                     "Yksls P Kznc:":"2,36",
                     "Yon Aci:":"9,1",
                     "Yon D Kznc:":"7,5",
                     "Yon I Kznc:":"3,6",
                     "Yon P Kznc:":"45.23"
                  }
               ],
               "Servo":[
                  {
                     "Alt Nkt:":"7,32",
                     "Motor Yon :":"23,63",
                     "Sag Ana Knt Aci orta:":"5,3",
                     "Sag Ana Knt Alt Nkt:":"3,2",
                     "Sag Ana Knt Orta Nkt:":"7,5",
                     "Sag Ana Knt Ust Nkt:":"9,6",
                     "Sag Ana Knt Yon:":"1,2",
                     "Sag Arka Knt Aci orta:":"47,12",
                     "Sag Arka Knt Alt Nkt:":"23,5",
                     "Sag Arka Knt Orta Nkt:":"78,21",
                     "Sag Arka Knt Ust Nkt:":"12,25",
                     "Sag Arka Knt Yon:":"2,5",
                     "Sol Ana Knt Aci orta:":"7,5",
                     "Sol Ana Knt Alt Nkt:":"7,5",
                     "Sol Ana Knt Orta Nkt:":"1,2",
                     "Sol Ana Knt Ust Nkt:":"9,2",
                     "Sol Ana Knt Yon:":"4,5",
                     "Sol Arka Knt Aci orta:":"",
                     "Sol Arka Knt Alt Nkt:":"",
                     "Sol Arka Knt Orta Nkt:":"",
                     "Sol Arka Knt Ust Nkt:":"",
                     "Sol Arka Knt Yon:":"",
                     "Ust Nkt:":"77,23"
                  }
               ],
               "Seyir":[
                  {
                     "Mncnk Klks Btrm Sure:":"23,21",
                     "Mncnk Klks Btrm Yksklk:":"4,21",
                     "Motor Kpt Lmt ":"12,36",
                     "Perdo Hiz:":"231,56",
                     "Perdo Sakinma Aci":"12,25",
                     "Pitch Trim:":"2,3",
                     "Prst Rota Zmn Asim:":"7,36",
                     "Roll Trim:":"4,21",
                     "Seyir Hizi IAS:":"23,15",
                     "Seyir Zmn Asim:":"4,12",
                     "Stab Zmn Asim:":"45566",
                     "Yaw Trim:":"4,26"
                  }
               ]
            }
            
            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 12 Feb 2021, 05:56 last edited by
            #5

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

            "11,48855",

            This is a string, no double.

            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 12 Feb 2021, 06:20
            4
            • J.HilkJ J.Hilk
              12 Feb 2021, 05:55

              do not localize your json, stick with the norm and use a dot as a separator for your double/floats

              D Offline
              D Offline
              deleted286
              wrote on 12 Feb 2021, 06:11 last edited by
              #6

              @J-Hilk said in When reading QJsonArray it gives always 0:

              do not localize your json, stick with the norm and use a dot as a separator for your double/floats

              I use dot but it wont change anything. I cant read

              Christian EhrlicherC J 2 Replies Last reply 12 Feb 2021, 06:16
              0
              • D deleted286
                12 Feb 2021, 06:11

                @J-Hilk said in When reading QJsonArray it gives always 0:

                do not localize your json, stick with the norm and use a dot as a separator for your double/floats

                I use dot but it wont change anything. I cant read

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 12 Feb 2021, 06:16 last edited by
                #7

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

                I cant read

                Please read my post...

                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
                • D deleted286
                  12 Feb 2021, 06:11

                  @J-Hilk said in When reading QJsonArray it gives always 0:

                  do not localize your json, stick with the norm and use a dot as a separator for your double/floats

                  I use dot but it wont change anything. I cant read

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 12 Feb 2021, 06:16 last edited by
                  #8
                  This post is deleted!
                  1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher
                    12 Feb 2021, 05:56

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

                    "11,48855",

                    This is a string, no double.

                    D Offline
                    D Offline
                    deleted286
                    wrote on 12 Feb 2021, 06:20 last edited by
                    #9

                    @Christian-Ehrlicher but 5.0 is double? I've changed the value

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 12 Feb 2021, 06:22 last edited by Christian Ehrlicher 2 Dec 2021, 06:24
                      #10

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

                      but 5.0 is double?

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

                      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
                      4
                      • sierdzioS Offline
                        sierdzioS Offline
                        sierdzio
                        Moderators
                        wrote on 12 Feb 2021, 06:27 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 12 Feb 2021, 06:32
                        2
                        • sierdzioS sierdzio
                          12 Feb 2021, 06:27

                          @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 12 Feb 2021, 06:32 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 12 Feb 2021, 06:37 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 12 Feb 2021, 10:20 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 12 Feb 2021, 11:05
                              0
                              • JonBJ JonB
                                12 Feb 2021, 10:20

                                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 12 Feb 2021, 11:05 last edited by
                                #15

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

                                JonBJ 1 Reply Last reply 12 Feb 2021, 12:03
                                0
                                • sierdzioS sierdzio
                                  12 Feb 2021, 06:27

                                  @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 12 Feb 2021, 11:07 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 Offline
                                    Christian EhrlicherC Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on 12 Feb 2021, 11:09 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 12 Feb 2021, 11:18
                                    4
                                    • Christian EhrlicherC Christian Ehrlicher
                                      12 Feb 2021, 11:09

                                      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 12 Feb 2021, 11:18 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 12 Feb 2021, 11:26
                                      0
                                      • D deleted286
                                        12 Feb 2021, 11: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 Offline
                                        Pl45m4P Offline
                                        Pl45m4
                                        wrote on 12 Feb 2021, 11:26 last edited by Pl45m4 2 Dec 2021, 11:34
                                        #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 12 Feb 2021, 11:41 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

                                          1/25

                                          12 Feb 2021, 05:43

                                          • Login

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