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. QJsonDocument, array method to create QJsonArray?

QJsonDocument, array method to create QJsonArray?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.7k Views 2 Watching
  • 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.
  • SPlattenS Offline
    SPlattenS Offline
    SPlatten
    wrote on last edited by
    #1

    I have created an instance of QJsonDocument:

        QJsonDocument docJSON = QJsonDocument::fromJson(cpszJSON);
    

    I can see the docJSON has a method array, how do I extract the array from it and convert to an instance of QJsonArray.

    I've tried using it to initialise an instance of QJsonArray, but there are syntax errors.

    I want to get the number of elements in the array and pass each element.

    Kind Regards,
    Sy

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #4

      https://doc.qt.io/qt-5/qtcore-serialization-savegame-example.html

      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
      2
      • SPlattenS SPlatten

        I have created an instance of QJsonDocument:

            QJsonDocument docJSON = QJsonDocument::fromJson(cpszJSON);
        

        I can see the docJSON has a method array, how do I extract the array from it and convert to an instance of QJsonArray.

        I've tried using it to initialise an instance of QJsonArray, but there are syntax errors.

        I want to get the number of elements in the array and pass each element.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @SPlatten
        If your JSON source has arrays in it, your QJsonDocument::fromJson()-generated document should have QJsonArrays in it? And you walk/find them like any other JSON element.

        Use the QJsonDocument QJsonDocument::fromJson(const QByteArray &json, QJsonParseError *error = nullptr) to pass in a &error to get clues about syntax errors.

        1 Reply Last reply
        1
        • SPlattenS Offline
          SPlattenS Offline
          SPlatten
          wrote on last edited by
          #3

          @SPlatten said in QJsonDocument, array method to create QJsonArray?:

          stance of QJsonArray, but there are syntax errors.

          Is there any good example on how to extract the array from the document?

          Kind Regards,
          Sy

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            https://doc.qt.io/qt-5/qtcore-serialization-savegame-example.html

            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
            2
            • SPlattenS Offline
              SPlattenS Offline
              SPlatten
              wrote on last edited by
              #5

              Thank you, I see that there are functions isArray and toArray, what is the array function, I cannot see how to use that?

              Kind Regards,
              Sy

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                Hi,

                Do you mean the QJsonDocument::array method ?

                As the documentation says, it returns the array contained in the document.

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                1
                • SPlattenS Offline
                  SPlattenS Offline
                  SPlatten
                  wrote on last edited by
                  #7

                  Yes, I get that but you cannot assign the array the function returns to a QJsonArray.

                  Kind Regards,
                  Sy

                  JonBJ 1 Reply Last reply
                  0
                  • SPlattenS SPlatten

                    Yes, I get that but you cannot assign the array the function returns to a QJsonArray.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #8

                    @SPlatten
                    I don't understand. It returns the QJsonArray if the (top-level of) the QJsonDocument is a QJsonArray . Why do you want to assign that to a QJsonArray? If you really want to, there are things like QJsonArray::QJsonArray(const QJsonArray &other) or QJsonDocument::setArray(const QJsonArray &array), but not sure what you are trying to achieve.

                    1 Reply Last reply
                    0
                    • SPlattenS Offline
                      SPlattenS Offline
                      SPlatten
                      wrote on last edited by
                      #9

                      Try it yourself, I have, you can't there is a problem:

                          calling 'array' with incomplete return type 'QJsonArray'  variable has incomplete type 'QJsonArray'.
                      

                      Kind Regards,
                      Sy

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #10

                        You forgot

                        #include <QJsonArray>
                        

                        in the file where you use that method.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        2

                        • Login

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