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?
Forum Updated to NodeBB v4.3 + New Features

QJsonDocument, array method to create QJsonArray?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 2.4k 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.
  • S Offline
    S Offline
    SPlatten
    wrote on 12 Apr 2020, 10:29 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

    J 1 Reply Last reply 12 Apr 2020, 10:35
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 12 Apr 2020, 10:49 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
      • S SPlatten
        12 Apr 2020, 10:29

        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.

        J Offline
        J Offline
        JonB
        wrote on 12 Apr 2020, 10:35 last edited by JonB 4 Dec 2020, 10:37
        #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
        • S Offline
          S Offline
          SPlatten
          wrote on 12 Apr 2020, 10:42 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
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 12 Apr 2020, 10:49 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
            • S Offline
              S Offline
              SPlatten
              wrote on 12 Apr 2020, 11:27 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
              • S Offline
                S Offline
                SGaist
                Lifetime Qt Champion
                wrote on 12 Apr 2020, 18:55 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
                • S Offline
                  S Offline
                  SPlatten
                  wrote on 13 Apr 2020, 06:30 last edited by
                  #7

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

                  Kind Regards,
                  Sy

                  J 1 Reply Last reply 13 Apr 2020, 06:56
                  0
                  • S SPlatten
                    13 Apr 2020, 06:30

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

                    J Offline
                    J Offline
                    JonB
                    wrote on 13 Apr 2020, 06:56 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
                    • S Offline
                      S Offline
                      SPlatten
                      wrote on 13 Apr 2020, 08:00 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
                      • S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 13 Apr 2020, 08:28 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

                        1/10

                        12 Apr 2020, 10:29

                        • Login

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