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. Parse nested JSON with Qt5

Parse nested JSON with Qt5

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 963 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.
  • V Offline
    V Offline
    ven1ceBeach
    wrote on 22 Mar 2021, 13:43 last edited by ven1ceBeach
    #1

    Hello,

    I am sending a GET request to openweathermap and store the JSON response in a QByteArray.

    19463b3d-f255-4fb4-ae82-5c9d142bba22-image.png
    (This string is stored in QByteArray)

    Unfortunately I do not know how to access the nested elements in "weather".
    This is the code I got:

    QByteArray val = reply->readAll();
    QJsonDocument JsonDoc = QJsonDocument::fromJson(val);
    QJsonObject JsonObj = JsonDoc.object();
    QJsonValue value = JsonObj.value(QString("weather"));
    qWarning() << value;
    

    Console Output

    QJsonValue(array, QJsonArray([{"description":"broken clouds","icon":"04d","id":803,"main":"Clouds"}]))
    

    How can I access the nested elements in the QJsonArray?

    Kind Regards

    J K 2 Replies Last reply 22 Mar 2021, 13:46
    0
    • V ven1ceBeach
      23 Mar 2021, 08:51

      @KroMignon

      Thank you, but when I try to use the toArray method, I get the following error:

      calling 'toarray' with incomplete return type 'qjsonarray'
      
      J Online
      J Online
      JonB
      wrote on 23 Mar 2021, 08:55 last edited by
      #6

      @ven1ceBeach said in Parse nested JSON with Qt5:

      calling 'toarray' with incomplete return type 'qjsonarray'

      I very much doubt that is a copy & paste of the actual error message. It really does help if you copy error messages, not type in your own characters....

      From the sound of the message, you have not done the necessary #include(s). Where do you have the required #include <QJsonArray>?

      1 Reply Last reply
      2
      • V ven1ceBeach
        22 Mar 2021, 13:43

        Hello,

        I am sending a GET request to openweathermap and store the JSON response in a QByteArray.

        19463b3d-f255-4fb4-ae82-5c9d142bba22-image.png
        (This string is stored in QByteArray)

        Unfortunately I do not know how to access the nested elements in "weather".
        This is the code I got:

        QByteArray val = reply->readAll();
        QJsonDocument JsonDoc = QJsonDocument::fromJson(val);
        QJsonObject JsonObj = JsonDoc.object();
        QJsonValue value = JsonObj.value(QString("weather"));
        qWarning() << value;
        

        Console Output

        QJsonValue(array, QJsonArray([{"description":"broken clouds","icon":"04d","id":803,"main":"Clouds"}]))
        

        How can I access the nested elements in the QJsonArray?

        Kind Regards

        J Online
        J Online
        jsulm
        Lifetime Qt Champion
        wrote on 22 Mar 2021, 13:46 last edited by
        #2

        @ven1ceBeach Well, you access each element of the array using https://doc.qt.io/qt-5/qjsonarray.html#operator-5b-5d and then access the content of each element depending on it's type. In your case you have objects as elements in your array.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • V ven1ceBeach
          22 Mar 2021, 13:43

          Hello,

          I am sending a GET request to openweathermap and store the JSON response in a QByteArray.

          19463b3d-f255-4fb4-ae82-5c9d142bba22-image.png
          (This string is stored in QByteArray)

          Unfortunately I do not know how to access the nested elements in "weather".
          This is the code I got:

          QByteArray val = reply->readAll();
          QJsonDocument JsonDoc = QJsonDocument::fromJson(val);
          QJsonObject JsonObj = JsonDoc.object();
          QJsonValue value = JsonObj.value(QString("weather"));
          qWarning() << value;
          

          Console Output

          QJsonValue(array, QJsonArray([{"description":"broken clouds","icon":"04d","id":803,"main":"Clouds"}]))
          

          How can I access the nested elements in the QJsonArray?

          Kind Regards

          K Offline
          K Offline
          KroMignon
          wrote on 22 Mar 2021, 13:48 last edited by
          #3

          @ven1ceBeach said in Parse nested JSON with Qt5:

          How can I access the nested elements in the QJsonArray?

          You got all infos you need here!
          I think you want to get access to first item of the array:

          QByteArray val = reply->readAll();
          const auto jsonRoot = QJsonDocument::fromJson(val).object();
          const auto weather = jsonRoot["weather"].toArray().first().toObject();
          

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

          V 1 Reply Last reply 23 Mar 2021, 08:51
          0
          • K KroMignon
            22 Mar 2021, 13:48

            @ven1ceBeach said in Parse nested JSON with Qt5:

            How can I access the nested elements in the QJsonArray?

            You got all infos you need here!
            I think you want to get access to first item of the array:

            QByteArray val = reply->readAll();
            const auto jsonRoot = QJsonDocument::fromJson(val).object();
            const auto weather = jsonRoot["weather"].toArray().first().toObject();
            
            V Offline
            V Offline
            ven1ceBeach
            wrote on 23 Mar 2021, 08:51 last edited by
            #4

            @KroMignon

            Thank you, but when I try to use the toArray method, I get the following error:

            calling 'toarray' with incomplete return type 'qjsonarray'
            
            J 1 Reply Last reply 23 Mar 2021, 08:55
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 23 Mar 2021, 08:55 last edited by
              #5

              Please post the correct error message instead typing it out of your memory.

              If you want to use a class (in this case QJsonArray) you have to include the appropriate Header so this class is known to the compiler.

              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
              • V ven1ceBeach
                23 Mar 2021, 08:51

                @KroMignon

                Thank you, but when I try to use the toArray method, I get the following error:

                calling 'toarray' with incomplete return type 'qjsonarray'
                
                J Online
                J Online
                JonB
                wrote on 23 Mar 2021, 08:55 last edited by
                #6

                @ven1ceBeach said in Parse nested JSON with Qt5:

                calling 'toarray' with incomplete return type 'qjsonarray'

                I very much doubt that is a copy & paste of the actual error message. It really does help if you copy error messages, not type in your own characters....

                From the sound of the message, you have not done the necessary #include(s). Where do you have the required #include <QJsonArray>?

                1 Reply Last reply
                2

                6/6

                23 Mar 2021, 08:55

                • Login

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