0

Im trying to read json file with qt c++. After reading i will do some operations. I've read my json like it, but i want to print spesific datas, which include "float". After finding them, i will assign a value each of these. Thanks for any help

{
   "A":[
      {
         "GPS":[
            {
               "GPS ID":[
                  "integer",
                  "0"
               ],
               "GPS Mod":[
                  "integer",
                  "1"
               ],
               "GPS Utc":[
                  "float",
                  "2"
               ],
               "GPS Latitude":[
                  "float",
                  "3"
               ],
               "GPS Longitude":[
                  "float",
                  "4"
               ]
  
            }
         ]

Here is what I have tried up to now:

  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.at(i).toObject();
  
   }
2
  • All you need is specified in the Qt documentation. Commented Mar 22, 2021 at 12:02
  • Thank you, it it helped a lot.... Commented Mar 22, 2021 at 12:11

1 Answer 1

1

Actually, you seem to already know what you need to do to handle Json data, you just have to continue to unpack your nested structures until the leaf data.

I'm not sure what was your issue, you just had to continue what you were doing but anyway, I have written an example that prints in the console the inner GPS data of the following structure (the one that you provided, I just added the missing braces and brackets):

{
   "A":
   [
        {
            "GPS":
            [
                {
                    "GPS ID":[
                        "integer",
                        "0"
                    ],
                    "GPS Mod":[
                        "integer",
                        "1"
                    ],
                    "GPS Utc":[
                        "float",
                        "2"
                    ],
                    "GPS Latitude":[
                        "float",
                        "3"
                    ],
                    "GPS Longitude":[
                        "float",
                        "4"
                    ]
                }
            ]
        }
    ]
}

Here is the example function:

int printJsonData(const QByteArray & json_data)
{
    QJsonParseError err;
    QJsonDocument doc = QJsonDocument::fromJson(json_data, &err);

    if(err.error != QJsonParseError::NoError)
        return -1; // Failure

    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)
            {
                std::cout << cit.key().toStdString() << ": (";

                QJsonArray inner_data = cit.value().toArray();
                for(int k = 0; k < inner_data.size(); ++k)
                {
                    std::cout << inner_data[k].toString().toStdString() << (k < inner_data.size()-1 ? "," : "");
                }
                std::cout << ")\n";
            }
        }
    }

    return 0;
}

Output:

GPS ID: (integer,0)
GPS Latitude: (float,3)
GPS Longitude: (float,4)
GPS Mod: (integer,1)
GPS Utc: (float,2)

Please note that you really should use the methods QJsonObject::contains(), QJsonValue::isArray(), QJsonValue::isObject() and QJsonValue::isString() that Qt provides in order to check that the data you are extracting are exactly what you expect them to be. I didn't write such checks in the example because it is just an example and I wanted to avoid clutter and make the code less readable.

Sign up to request clarification or add additional context in comments.

6 Comments

thank you for your help. I just want to see float, integer part. but i cant do it with editing this code
@süslücoder Of course you can ! The type values you are looking for are the first element (index 0) of the arrays inner_data. You just have to remove the for loop and replace k by 0.
i want to ask something, you see i have different numbers for each, like GPS Latitude: (float,3) how can i reach 3
@süslücoder This is the same thing... except that the index is 1 instead of 0. Are you sure you understand C++ ? I don't want to be mean but, this is quite basic. If you don't understand the code I wrote, I didn't help you by writing it for you.
Hi, i added 2 more array and use inner_data[2], but the code doesnt see them
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.