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
Forum Updated to NodeBB v4.3 + New Features

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.
  • JonBJ JonB

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

    How can i reach specificly Gps id 0 which is the 1st element of GPS Id array.

    // recognise "GPS ID" among `for(QJsonObject::const_iterator cit = gps_obj.constBegin()`
    // you cannot recognise "GPS ID" as the "first" item in the object in the "GPS" array,
    // as JSON keys in an object have no order
    if (cit.key() == "GPS ID")
    
    // or, access it directly:
    cit = gps_obj["GPS ID"]
    
    QJsonArray inner_data = cit.value().toArray();
    inner_data[0];  // "integer"
    inner_data[1];  // "0"
    
    D Offline
    D Offline
    deleted286
    wrote on last edited by
    #3

    @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.HilkJ JonBJ 2 Replies Last reply
    0
    • D deleted286

      @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.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on 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
      2
      • J.HilkJ J.Hilk

        @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 last edited by
        #5

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

        1 Reply Last reply
        1
        • D deleted286

          @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

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on 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
          1
          • JonBJ JonB

            @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 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"
            
                                 }
            
            
                                }
            
            JonBJ 1 Reply Last reply
            0
            • D deleted286

              @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"
              
                                   }
              
              
                                  }
              
              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on 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
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 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

                JonBJ 1 Reply Last reply
                3
                • JonBJ JonB

                  @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 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
                  
                  JonBJ 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

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

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on 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
                    0
                    • D deleted286

                      @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
                      
                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on 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
                      • JonBJ JonB

                        @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 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

                        • Login

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