Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved How to parse such a complex json file

    General and Desktop
    qt5 json read
    4
    10
    724
    Loading More Posts
    • 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 last edited by

      How can i parse such a file?
      I have written the reading part but i cant imagine the other parts

      {
          "Plan": [
              {
                  "baslangic nokta": 0,
                  "rota no": 0,
                  "toplam nokta": 3
              },
              {
                  "Boylam": 32.7088737487793,
                  "Enlem": 40.00210952758789,
                  "görev": 0,
                  "irtifa": 2500,
                  "no": 0,
                  "sonraki": 1,
                  "yaricap": 0
              },
              {
                  "Boylam": 32.715911865234375,
                  "Enlem": 39.97725296020508,
                  "görev": 0,
                  "irtifa": 2500,
                  "no": 1,
                  "sonraki": 2,
                  "yaricap": 0
              },
              {
                  "Boylam": 32.74869918823242,
                  "Enlem": 40.003292083740234,
                  "görev": 0,
                  "irtifa": 2500,
                  "no": 2,
                  "sonraki": 0,
                  "yaricap": 0
              }
          ]
      }
      
      ```
      QFile file("/home/test.json");
      file.open(QIODevice::ReadOnly | QIODevice::Text);
      QByteArray data = file.readAll();
      file.close();
      
      QJsonParseError error;
      QJsonDocument doc = QJsonDocument::fromJson(data, &error);
      if(doc.isNull())
      {
          qDebug() << "parse failed";
      }
      
      
      QJsonObject rootObj = doc.object();
      
      KroMignon 1 Reply Last reply Reply Quote 0
      • Christian Ehrlicher
        Christian Ehrlicher Lifetime Qt Champion last edited by

        Since you create exactly this structure here with the help of Qt's json classes - what's the problem doing it the other way round?

        Qt has to stay free or it will die.

        1 Reply Last reply Reply Quote 1
        • KroMignon
          KroMignon @deleted286 last edited by

          @suslucoder said in How to parse such a complex json file:

          How can i parse such a file?

          Parsing is already done by QJsonDocument::fromJson().

          It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

          D 1 Reply Last reply Reply Quote 1
          • D
            deleted286 @KroMignon last edited by

            @KroMignon said in How to parse such a complex json file:

            Parsing is already done by QJsonDocument::fromJson().

            Yes but for using the datas on the file, i want to to such a thing:
            is this the right approach?

            ```
              if(rootObj.contains("Plan"))
                {
                   QJsonObject subObj = rootObj.value("Plan").toObject();
                   qDebug() << subObj.value("enlem").toString();
                   qDebug() << subObj.value("boylam").toString();
            

            }

            KroMignon 1 Reply Last reply Reply Quote 0
            • Christian Ehrlicher
              Christian Ehrlicher Lifetime Qt Champion last edited by

              Simply try it out?

              Qt has to stay free or it will die.

              1 Reply Last reply Reply Quote 0
              • KroMignon
                KroMignon @deleted286 last edited by KroMignon

                @suslucoder said in How to parse such a complex json file:

                is this the right approach?

                Half good!

                With Json, you have to know the structure of the document you want to read.
                Qt gives you some classes to help you:

                • QJsonDocument to read/write a full Json entity
                • QJsonValue: encapsulates a JSON value
                • QJsonObject: encapsulates a JSON object
                • QJsonArray: encapsulates a JSON array

                So you start with parsing the JSON string with QJsonDocument::fromJson(), then get the Root item with:

                • QJsonDocument::toObject() if root is an object (like in your example)
                • QJsonDocument::toArray() if root is an array

                Then go through the structure:
                Your root object has one value called "Plan" with hold an array:
                QJsonArray planArray = root.value("plan").toArray();

                And the got through the array items

                for(const auto &value : planArray )
                {
                    QJsonObject object = value.ToObject();
                    ...
                }
                

                Hope this helps.

                It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                D 2 Replies Last reply Reply Quote 5
                • D
                  deleted286 @KroMignon last edited by

                  @KroMignon It helps me a lot. I read my json file. Thank you so much

                  1 Reply Last reply Reply Quote 0
                  • D
                    deleted286 @KroMignon last edited by

                    @KroMignon Hi. In here, how can i reach each element in the array. Since i have 2 root in my planArray, i want to achieve them differently.

                    KroMignon R 2 Replies Last reply Reply Quote 0
                    • KroMignon
                      KroMignon @deleted286 last edited by

                      @suslucoder said in How to parse such a complex json file:

                      Since i have 2 root in my planArray, i want to achieve them differently.

                      I don't understand this?!?

                      A JSON document can only have one root element: an object (which can have multiple values) or an array (which can have multiple objects).

                      It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

                      1 Reply Last reply Reply Quote 4
                      • R
                        Robert Hairgrove @deleted286 last edited by

                        @suslucoder The "Plan" array is a little unusual because the objects it contains are not all of the same structure. Usually, arrays should contain a sequence of elements of the same type. However, it is legal JSON.

                        If you are certain that the first element of the "Plan" array will have the structure as in your example above, you can perhaps rely on the others to all have the 2nd type structure. Otherwise, you might need to check for the existence of certain keys -- for example, by using the function QJsonObject::contains() to determine which kind of object it is.

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post