Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Reach the spesific element of Json Array



  • 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 ? "," : "");
                        }
    
                }
    
            }
    
        }


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


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


  • Moderators

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

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



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



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


  • @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"
    
                         }
    
    
                        }
    


  • @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();
        ...
    }
    

  • Lifetime Qt Champion

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



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


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



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



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


Log in to reply