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 Offline
    D Offline
    deleted286
    wrote on last edited by
    #1

    Here is my example Json File. I've read my json given the code before. But i have a question.
    How can i reach specificly Gps id 0 which is the 1st element of GPS Id array.
    I've try to keep that values in a vector, and then get but it will be too complicated.
    Im open to any suggestion

    {
       "A":[
          {
             "GPS":[
                {
                   "GPS ID":[
                      "integer",
                      "0"
                   ],
                   "GPS Mod":[
                      "integer",
                      "1"
                   ],
                   "GPS Utc":[
                      "float",
                      "2"
                   ],
                   "GPS Latitude":[
                      "float",
                      "3"
                   ],
                   "GPS Longitude":[
                      "float",
                      "4"
                   ]
      
                }
             ]
    
        QByteArray data = file.readAll();
        QJsonDocument doc = QJsonDocument::fromJson(data);
    
        QJsonObject root = doc.object();
        QJsonArray tlmtArray = root.value("A").toArray();
    
        for(int i = 0; i < tlmtArray.size(); ++i)
           {
               QJsonObject obj = tlmtArray[i].toObject();
               QJsonArray gps_array = obj.value("GPS").toArray();
    
        for(int j = 0; j < gps_array.size(); ++j)
            {
                QJsonObject gps_obj = gps_array[j].toObject();
    
                for(QJsonObject::const_iterator cit = gps_obj.constBegin(); cit != gps_obj.constEnd(); ++cit)
                {
                   // qDebug() << cit.key().toStdU16String() <<  ": (";
    
                    QJsonArray inner_data = cit.value().toArray();
    
                        if(inner_data[0] == "float")
                        {
                           // qDebug() << inner_data[0].toString().toStdU16String() << (0 < inner_data.size()-1 ? "," : "");
    
                        QVector<int> index;
                        index << inner_data[1].toString().toInt();
                      //  qDebug() << index;
                        if(index[0] ==5 )
                        {
                            qDebug() << index[0];
                            gps[index[0]].f;
                        }
    
                        }
    
    
                       else if (inner_data[0] == "integer")
                        {
    
                           // qDebug() << inner_data[0].toString().toStdU16String() << (0 < inner_data.size()-1 ? "," : "");
                        }
    
                }
    
            }
    
        }
    
    JonBJ 1 Reply Last reply
    0
    • D deleted286

      Here is my example Json File. I've read my json given the code before. But i have a question.
      How can i reach specificly Gps id 0 which is the 1st element of GPS Id array.
      I've try to keep that values in a vector, and then get but it will be too complicated.
      Im open to any suggestion

      {
         "A":[
            {
               "GPS":[
                  {
                     "GPS ID":[
                        "integer",
                        "0"
                     ],
                     "GPS Mod":[
                        "integer",
                        "1"
                     ],
                     "GPS Utc":[
                        "float",
                        "2"
                     ],
                     "GPS Latitude":[
                        "float",
                        "3"
                     ],
                     "GPS Longitude":[
                        "float",
                        "4"
                     ]
        
                  }
               ]
      
          QByteArray data = file.readAll();
          QJsonDocument doc = QJsonDocument::fromJson(data);
      
          QJsonObject root = doc.object();
          QJsonArray tlmtArray = root.value("A").toArray();
      
          for(int i = 0; i < tlmtArray.size(); ++i)
             {
                 QJsonObject obj = tlmtArray[i].toObject();
                 QJsonArray gps_array = obj.value("GPS").toArray();
      
          for(int j = 0; j < gps_array.size(); ++j)
              {
                  QJsonObject gps_obj = gps_array[j].toObject();
      
                  for(QJsonObject::const_iterator cit = gps_obj.constBegin(); cit != gps_obj.constEnd(); ++cit)
                  {
                     // qDebug() << cit.key().toStdU16String() <<  ": (";
      
                      QJsonArray inner_data = cit.value().toArray();
      
                          if(inner_data[0] == "float")
                          {
                             // qDebug() << inner_data[0].toString().toStdU16String() << (0 < inner_data.size()-1 ? "," : "");
      
                          QVector<int> index;
                          index << inner_data[1].toString().toInt();
                        //  qDebug() << index;
                          if(index[0] ==5 )
                          {
                              qDebug() << index[0];
                              gps[index[0]].f;
                          }
      
                          }
      
      
                         else if (inner_data[0] == "integer")
                          {
      
                             // qDebug() << inner_data[0].toString().toStdU16String() << (0 < inner_data.size()-1 ? "," : "");
                          }
      
                  }
      
              }
      
          }
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

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