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. How to parse such a complex json file
QtWS25 Last Chance

How to parse such a complex json file

Scheduled Pinned Locked Moved Solved General and Desktop
qt5jsonread
10 Posts 4 Posters 2.7k 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 5 Feb 2021, 13:13 last edited by
    #1

    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();
    
    K 1 Reply Last reply 5 Feb 2021, 13:16
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 5 Feb 2021, 13:15 last edited by
      #2

      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 Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • D deleted286
        5 Feb 2021, 13:13

        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();
        
        K Offline
        K Offline
        KroMignon
        wrote on 5 Feb 2021, 13:16 last edited by
        #3

        @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 5 Feb 2021, 13:26
        1
        • K KroMignon
          5 Feb 2021, 13:16

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

          D Offline
          D Offline
          deleted286
          wrote on 5 Feb 2021, 13:26 last edited by
          #4

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

          }

          K 1 Reply Last reply 5 Feb 2021, 13:50
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 5 Feb 2021, 13:27 last edited by
            #5

            Simply try it out?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0
            • D deleted286
              5 Feb 2021, 13:26

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

              }

              K Offline
              K Offline
              KroMignon
              wrote on 5 Feb 2021, 13:50 last edited by KroMignon 2 May 2021, 13:50
              #6

              @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 5 Feb 2021, 14:05
              5
              • K KroMignon
                5 Feb 2021, 13:50

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

                D Offline
                D Offline
                deleted286
                wrote on 5 Feb 2021, 14:05 last edited by
                #7

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

                1 Reply Last reply
                0
                • K KroMignon
                  5 Feb 2021, 13:50

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

                  D Offline
                  D Offline
                  deleted286
                  wrote on 8 Feb 2021, 14:18 last edited by
                  #8

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

                  K R 2 Replies Last reply 8 Feb 2021, 14:55
                  0
                  • D deleted286
                    8 Feb 2021, 14:18

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

                    K Offline
                    K Offline
                    KroMignon
                    wrote on 8 Feb 2021, 14:55 last edited by
                    #9

                    @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
                    4
                    • D deleted286
                      8 Feb 2021, 14:18

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

                      R Offline
                      R Offline
                      Robert Hairgrove
                      wrote on 9 Feb 2021, 22:43 last edited by
                      #10

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

                      3/10

                      5 Feb 2021, 13:16

                      topic:navigator.unread, 7
                      • Login

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