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. Reach the spesific element of Json Array

Reach the spesific element of Json Array

Scheduled Pinned Locked Moved Solved General and Desktop
qt5json
13 Posts 4 Posters 1.5k 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 deleted286
    23 Mar 2021, 08:08

    @JonB this works for me, but i want to ask something more, if it isnt problem

                     if (cit.key() == "GPS ID")
                     {
                         QJsonArray inner_data = cit.value().toArray();
                        qDebug() <<  inner_data[0];  // "integer"
                        qDebug() <<  inner_data[1];  // "0"
                     }
    

    My output is like:

    QJsonValue(string, "integer")
    QJsonValue(string, "0")
    

    Cant i get it just integer and 0

    J Offline
    J Offline
    J.Hilk
    Moderators
    wrote on 23 Mar 2021, 08:11 last edited by
    #4

    @suslucoder said in Reach the spesific element of Json Array:

    qDebug() << inner_data[0].toString(); // "integer"
    qDebug() << inner_data[1].toString(); // "0"


    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 23 Mar 2021, 08:12
    2
    • J J.Hilk
      23 Mar 2021, 08:11

      @suslucoder said in Reach the spesific element of Json Array:

      qDebug() << inner_data[0].toString(); // "integer"
      qDebug() << inner_data[1].toString(); // "0"

      D Offline
      D Offline
      deleted286
      wrote on 23 Mar 2021, 08:12 last edited by
      #5

      @J-Hilk i did it before you wrote and it works. Thank you

      1 Reply Last reply
      1
      • D deleted286
        23 Mar 2021, 08:08

        @JonB this works for me, but i want to ask something more, if it isnt problem

                         if (cit.key() == "GPS ID")
                         {
                             QJsonArray inner_data = cit.value().toArray();
                            qDebug() <<  inner_data[0];  // "integer"
                            qDebug() <<  inner_data[1];  // "0"
                         }
        

        My output is like:

        QJsonValue(string, "integer")
        QJsonValue(string, "0")
        

        Cant i get it just integer and 0

        J Offline
        J Offline
        JonB
        wrote on 23 Mar 2021, 08:16 last edited by
        #6

        @suslucoder
        QJsonValue is something which behaves like a QVariant. It can hold any of the (6) types returned by QJsonValue::Type QJsonValue::type() .

        You can use any of the QJsonValue::is...() methods to test for a particular type.

        If you know what your type is in advance, or you test it as above to make sure, for your case you can use:

        QJsonValue("integer").toString();  // `"integer"`, as a string
        QJsonValue("0").toInt();  // `0`, as an integer
        
        D 1 Reply Last reply 23 Mar 2021, 08:30
        1
        • J JonB
          23 Mar 2021, 08:16

          @suslucoder
          QJsonValue is something which behaves like a QVariant. It can hold any of the (6) types returned by QJsonValue::Type QJsonValue::type() .

          You can use any of the QJsonValue::is...() methods to test for a particular type.

          If you know what your type is in advance, or you test it as above to make sure, for your case you can use:

          QJsonValue("integer").toString();  // `"integer"`, as a string
          QJsonValue("0").toInt();  // `0`, as an integer
          
          D Offline
          D Offline
          deleted286
          wrote on 23 Mar 2021, 08:30 last edited by
          #7

          @JonB well, thank you so much

          What if i want to short this code... Im trying to give or operand like

          inner_data[1].toString().toInt()         // it print the 0 for 3 times and the others as well
          

          I know it isnt healthy for writing much more if statements but i foul up

          if (cit.key() == "GPS ID" || cit.key() == "GPS Mod" || cit.key() == "GPS Uydu Sayısı")
                                  {
                                      QJsonArray inner_data = cit.value().toArray();
                                      gps[inner_data[1].toString().toInt()].i;  // "0"
          
                                  }
          
                           ```
                          if (inner_data[0] == "integer")
                              {
          
                               if (cit.key() == "GPS ID)
                               {
                                   QJsonArray inner_data = cit.value().toArray();
                                   gps[inner_data[1].toString().toInt()].i;  // "0"
          
                               }
          
                               if (cit.key() == "GPS Mod")
                               {
                                   QJsonArray inner_data = cit.value().toArray();
                                   gps[inner_data[1].toString().toInt()];
          
                               }
          
                               if (cit.key() == "GPS Uydu Sayısı")
                               {
                                   QJsonArray inner_data = cit.value().toArray();
                                   gps[inner_data[1].toString().toInt()].i;  // "0"
          
                               }
          
          
                              }
          
          J 1 Reply Last reply 23 Mar 2021, 08:39
          0
          • D deleted286
            23 Mar 2021, 08:30

            @JonB well, thank you so much

            What if i want to short this code... Im trying to give or operand like

            inner_data[1].toString().toInt()         // it print the 0 for 3 times and the others as well
            

            I know it isnt healthy for writing much more if statements but i foul up

            if (cit.key() == "GPS ID" || cit.key() == "GPS Mod" || cit.key() == "GPS Uydu Sayısı")
                                    {
                                        QJsonArray inner_data = cit.value().toArray();
                                        gps[inner_data[1].toString().toInt()].i;  // "0"
            
                                    }
            
                             ```
                            if (inner_data[0] == "integer")
                                {
            
                                 if (cit.key() == "GPS ID)
                                 {
                                     QJsonArray inner_data = cit.value().toArray();
                                     gps[inner_data[1].toString().toInt()].i;  // "0"
            
                                 }
            
                                 if (cit.key() == "GPS Mod")
                                 {
                                     QJsonArray inner_data = cit.value().toArray();
                                     gps[inner_data[1].toString().toInt()];
            
                                 }
            
                                 if (cit.key() == "GPS Uydu Sayısı")
                                 {
                                     QJsonArray inner_data = cit.value().toArray();
                                     gps[inner_data[1].toString().toInt()].i;  // "0"
            
                                 }
            
            
                                }
            
            J Offline
            J Offline
            JonB
            wrote on 23 Mar 2021, 08:39 last edited by JonB
            #8

            @suslucoder
            I don't really know what your latest question is.

            For inner_data[1].toString().toInt() it is pointless to convert to string and then to integer, as I wrote earlier you might as well go inner_data[1].toInt().

            Yes you can do all 3 via

            if (cit.key() == "GPS ID" || cit.key() == "GPS Mod" || cit.key() == "GPS Uydu Sayısı")
            {
                QJsonArray inner_data = cit.value().toArray();
                int intValue = inner_data[1].toString().toInt();
                ...
            }
            
            D 1 Reply Last reply 23 Mar 2021, 08:46
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 23 Mar 2021, 08:43 last edited by
              #9

              @JonB said in Reach the spesific element of Json Array:

              wrote earlier you might as well go inner_data[1].toInt()

              No because the json contains a string, not an integer. The json is 'wrong' in this way - when an integer should be stored/read then it should be stored as integer and not as string.

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

              J 1 Reply Last reply 23 Mar 2021, 08:48
              3
              • J JonB
                23 Mar 2021, 08:39

                @suslucoder
                I don't really know what your latest question is.

                For inner_data[1].toString().toInt() it is pointless to convert to string and then to integer, as I wrote earlier you might as well go inner_data[1].toInt().

                Yes you can do all 3 via

                if (cit.key() == "GPS ID" || cit.key() == "GPS Mod" || cit.key() == "GPS Uydu Sayısı")
                {
                    QJsonArray inner_data = cit.value().toArray();
                    int intValue = inner_data[1].toString().toInt();
                    ...
                }
                
                D Offline
                D Offline
                deleted286
                wrote on 23 Mar 2021, 08:46 last edited by
                #10

                @JonB
                When i wrote it with ||

                and wants to print out

                inner_data[1].toString.toInt()
                

                I expect that it should give me

                0 
                1 
                13       like i do it with seperate if statements
                

                But it prints

                0 0 0
                1 1 1
                13 13 13
                
                J 1 Reply Last reply 23 Mar 2021, 08:51
                0
                • C Christian Ehrlicher
                  23 Mar 2021, 08:43

                  @JonB said in Reach the spesific element of Json Array:

                  wrote earlier you might as well go inner_data[1].toInt()

                  No because the json contains a string, not an integer. The json is 'wrong' in this way - when an integer should be stored/read then it should be stored as integer and not as string.

                  J Offline
                  J Offline
                  JonB
                  wrote on 23 Mar 2021, 08:48 last edited by JonB
                  #11

                  @Christian-Ehrlicher
                  You are right. I had not fully noticed the "0" string-type (and similarly with the other numbers).

                  @suslucoder
                  Whatever produces our JSON (you? something else?) would be better outputting the numbers as numbers (no quotes) rather than as strings.

                  If the JSON had

                  "GPS ID":[
                                    "integer",
                                    0
                                 ],
                  

                  then you could go inner_data[1].toInt(). However if it is

                  "GPS ID":[
                                    "integer",
                                    "0"
                                 ],
                  

                  then you must indeed go inner_data[1].toString().toInt(). I have corrected my earlier accordingly.

                  D 1 Reply Last reply 23 Mar 2021, 09:01
                  0
                  • D deleted286
                    23 Mar 2021, 08:46

                    @JonB
                    When i wrote it with ||

                    and wants to print out

                    inner_data[1].toString.toInt()
                    

                    I expect that it should give me

                    0 
                    1 
                    13       like i do it with seperate if statements
                    

                    But it prints

                    0 0 0
                    1 1 1
                    13 13 13
                    
                    J Offline
                    J Offline
                    JonB
                    wrote on 23 Mar 2021, 08:51 last edited by
                    #12

                    @suslucoder said in Reach the spesific element of Json Array:

                    I expect that it should give me

                    That is what it will do.

                    But it prints

                    Sounds like you are printing the same value 3 times each time. No idea why you would do that, or what you would expect. If you have 3 prints (or 9 prints), that is what you will get....

                    1 Reply Last reply
                    0
                    • J JonB
                      23 Mar 2021, 08:48

                      @Christian-Ehrlicher
                      You are right. I had not fully noticed the "0" string-type (and similarly with the other numbers).

                      @suslucoder
                      Whatever produces our JSON (you? something else?) would be better outputting the numbers as numbers (no quotes) rather than as strings.

                      If the JSON had

                      "GPS ID":[
                                        "integer",
                                        0
                                     ],
                      

                      then you could go inner_data[1].toInt(). However if it is

                      "GPS ID":[
                                        "integer",
                                        "0"
                                     ],
                      

                      then you must indeed go inner_data[1].toString().toInt(). I have corrected my earlier accordingly.

                      D Offline
                      D Offline
                      deleted286
                      wrote on 23 Mar 2021, 09:01 last edited by
                      #13

                      @JonB I produced my Json. It make sense i will edit my json and write without " "

                      1 Reply Last reply
                      0

                      13/13

                      23 Mar 2021, 09:01

                      • Login

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