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 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?
    
            }
    
    JonBJ Gojir4G 2 Replies Last reply
    0
    • D Dy3zz

      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?
      
              }
      
      Gojir4G Offline
      Gojir4G Offline
      Gojir4
      wrote on 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
      3
      • D Dy3zz

        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?
        
                }
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on 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

          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?
          
                  }
          
          Gojir4G Offline
          Gojir4G Offline
          Gojir4
          wrote on 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
          3
          • Gojir4G Gojir4

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

            Gojir4G 1 Reply Last reply
            0
            • D Dy3zz

              @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

              Gojir4G Offline
              Gojir4G Offline
              Gojir4
              wrote on last edited by
              #5

              @Dy3zz You need to add #include <QJsonArray>

              D 1 Reply Last reply
              1
              • Gojir4G Gojir4

                @Dy3zz You need to add #include <QJsonArray>

                D Offline
                D Offline
                Dy3zz
                wrote on last edited by
                #6

                @Gojir4 Thanks it worked.

                1 Reply Last reply
                0

                • Login

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