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 get elements from json array
Forum Update on Monday, May 27th 2025

How to get elements from json array

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 4.2k 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
    Dy3zz
    wrote on 30 Nov 2021, 13:08 last edited by
    #1

    I have the next JSON:

    {"Number of people":3,"person":["Marian Marian","Badea Valentin","Ion Andrei"]}
    

    response1 is my string with json format, and i want to print every person name. i tried jsonObject["person"][i] but it doesn't work. any help?

            QString qstr = QString::fromStdString(response1);
            QJsonDocument jsonResponse = QJsonDocument::fromJson(qstr.toUtf8());
            QJsonObject jsonObject = jsonResponse.object();
            int nr_rows=jsonObject["Number of people"].toInt();
            
            for(int i=0;i<nr_rows;i++){
                cout<<jsonObject["person"][i]; // how to print every element?
    
            }
    
    J G 2 Replies Last reply 30 Nov 2021, 13:18
    0
    • D Dy3zz
      30 Nov 2021, 13:08

      I have the next JSON:

      {"Number of people":3,"person":["Marian Marian","Badea Valentin","Ion Andrei"]}
      

      response1 is my string with json format, and i want to print every person name. i tried jsonObject["person"][i] but it doesn't work. any help?

              QString qstr = QString::fromStdString(response1);
              QJsonDocument jsonResponse = QJsonDocument::fromJson(qstr.toUtf8());
              QJsonObject jsonObject = jsonResponse.object();
              int nr_rows=jsonObject["Number of people"].toInt();
              
              for(int i=0;i<nr_rows;i++){
                  cout<<jsonObject["person"][i]; // how to print every element?
      
              }
      
      G Offline
      G Offline
      Gojir4
      wrote on 30 Nov 2021, 13:19 last edited by
      #3

      @Dy3zz You need to use QJsonArray interface

      QJsonArray a = jsonObject["person"].toArray();
      for (int i = 0; i < a.size(); i++){
      cout << a.at(i).toString();
      }

      D 1 Reply Last reply 30 Nov 2021, 13:25
      3
      • D Dy3zz
        30 Nov 2021, 13:08

        I have the next JSON:

        {"Number of people":3,"person":["Marian Marian","Badea Valentin","Ion Andrei"]}
        

        response1 is my string with json format, and i want to print every person name. i tried jsonObject["person"][i] but it doesn't work. any help?

                QString qstr = QString::fromStdString(response1);
                QJsonDocument jsonResponse = QJsonDocument::fromJson(qstr.toUtf8());
                QJsonObject jsonObject = jsonResponse.object();
                int nr_rows=jsonObject["Number of people"].toInt();
                
                for(int i=0;i<nr_rows;i++){
                    cout<<jsonObject["person"][i]; // how to print every element?
        
                }
        
        J Online
        J Online
        JonB
        wrote on 30 Nov 2021, 13:18 last edited by
        #2

        @Dy3zz
        Your array is a QJsonArray. I can't quite recall the syntax (not tested), but you'll want something like

        jsonObject["person"].toArray()[i]
        
        1 Reply Last reply
        1
        • D Dy3zz
          30 Nov 2021, 13:08

          I have the next JSON:

          {"Number of people":3,"person":["Marian Marian","Badea Valentin","Ion Andrei"]}
          

          response1 is my string with json format, and i want to print every person name. i tried jsonObject["person"][i] but it doesn't work. any help?

                  QString qstr = QString::fromStdString(response1);
                  QJsonDocument jsonResponse = QJsonDocument::fromJson(qstr.toUtf8());
                  QJsonObject jsonObject = jsonResponse.object();
                  int nr_rows=jsonObject["Number of people"].toInt();
                  
                  for(int i=0;i<nr_rows;i++){
                      cout<<jsonObject["person"][i]; // how to print every element?
          
                  }
          
          G Offline
          G Offline
          Gojir4
          wrote on 30 Nov 2021, 13:19 last edited by
          #3

          @Dy3zz You need to use QJsonArray interface

          QJsonArray a = jsonObject["person"].toArray();
          for (int i = 0; i < a.size(); i++){
          cout << a.at(i).toString();
          }

          D 1 Reply Last reply 30 Nov 2021, 13:25
          3
          • G Gojir4
            30 Nov 2021, 13:19

            @Dy3zz You need to use QJsonArray interface

            QJsonArray a = jsonObject["person"].toArray();
            for (int i = 0; i < a.size(); i++){
            cout << a.at(i).toString();
            }

            D Offline
            D Offline
            Dy3zz
            wrote on 30 Nov 2021, 13:25 last edited by
            #4

            @Gojir4 said in How to get elements from json array:

            QJsonArray a = jsonObject["person"].toArray();

            I tried it but i have an error: calling toArray with incomplete return type

            G 1 Reply Last reply 30 Nov 2021, 13:26
            0
            • D Dy3zz
              30 Nov 2021, 13:25

              @Gojir4 said in How to get elements from json array:

              QJsonArray a = jsonObject["person"].toArray();

              I tried it but i have an error: calling toArray with incomplete return type

              G Offline
              G Offline
              Gojir4
              wrote on 30 Nov 2021, 13:26 last edited by
              #5

              @Dy3zz You need to add #include <QJsonArray>

              D 1 Reply Last reply 30 Nov 2021, 13:29
              1
              • G Gojir4
                30 Nov 2021, 13:26

                @Dy3zz You need to add #include <QJsonArray>

                D Offline
                D Offline
                Dy3zz
                wrote on 30 Nov 2021, 13:29 last edited by
                #6

                @Gojir4 Thanks it worked.

                1 Reply Last reply
                0

                3/6

                30 Nov 2021, 13:19

                • Login

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