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. Qt Parsing Json Object
Forum Update on Monday, May 27th 2025

Qt Parsing Json Object

Scheduled Pinned Locked Moved Unsolved General and Desktop
9 Posts 6 Posters 5.5k 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.
  • N Offline
    N Offline
    Naresh
    wrote on last edited by
    #1

    I am trying to parse json object in QT from an api. However when i try the codes written below i cannot parse the object. I simply want to get the information stored in those parameters. Please help me thank you !!!

    API is

    {
        "error": {
            "errorcode": 0,
            "errorstring": ""
        },
        "login": 1,
        "logintoken": "209b06aa7f059673db393ff7a731af1847344903b9733f026cc3a6f7a5b797b3"
    }
    

    The Codes are

     QNetworkReply* reply = manager->post(ava_request,postData);
    loop.exec();
    
    QString response = (QString)reply->readAll();
    qDebug() << response;
    
    QJsonDocument temp = QJsonDocument::fromJson(response.toUtf8());
    
     QJsonObject jsonObj = temp.object();
     qDebug() <<"error"<< jsonObj["error"].toString();
     qDebug() <<"login"<< jsonObj["login"].toString();
     qDebug() << "logintoken"<<jsonObj["logintoken"].toString();
    

    the response string looks

    and the output is

    D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:45 (QString NetworkConnection::Connect(QString, QString)): "<br><br>NO OF ROWDATA: 1{\"error\":{\"errorcode\":0,\"errorstring\":\"\"},\"login\":1,\"logintoken\":\"4daaf6b3dd5a26a2ad2c436e564bfa4d6c439ce4d0d6cd66705a8bdadddddaa0\"}"
    D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:50 (QString NetworkConnection::Connect(QString, QString)): error ""
    D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:51 (QString NetworkConnection::Connect(QString, QString)): login ""
    D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:52 (QString NetworkConnection::Connect(QString, QString)): logintoken ""
    
    JKSHJ 1 Reply Last reply
    0
    • dheerendraD Offline
      dheerendraD Offline
      dheerendra
      Qt Champions 2022
      wrote on last edited by
      #2

      Your JSON is mix of array & objects. You need split them properly. I have given the code here. It is not optimised.

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
          MyWidget w;
          w.show();
      
          QString json = "{\"error\":{\"errorcode\":0,\"errorstring\":\"\"},\"login\":1,\"logintoken\":\"4daaf6b3dd5a26a2ad2c436e564bfa4d6c439ce4d0d6cd66705a8bdadddddaa0\"}";
      
          QJsonDocument temp = QJsonDocument::fromJson(json.toUtf8());
          if (temp.isArray()){
              qDebug() << " It is an array" <<endl;
          }
          if (temp.isObject()){
              qDebug() << " It is an Object" <<endl;
          }
      
          QJsonArray arr  = temp.array();
          qDebug() << " Array size = "  << arr.size() <<endl;
      
          QJsonObject obj  = temp.object();
          qDebug() << " Object size = "  << obj.size() <<endl;
          qDebug() << " Object size = "  << obj.keys() <<endl;
          QJsonObject::iterator it;
          for(it=obj.begin();it!=obj.end();it++){
              if(it.value().isObject()){
                 QJsonObject o1 = it.value().toObject();
                 qDebug() << " it is JSON Object" <<endl;
                 qDebug() << " Key = " << it.key() << " Val =" << it.value() <<endl;
                 QJsonObject::iterator it1 = o1.begin();
                 for(it1=o1.begin();it1!=o1.end();it1++){
                     qDebug() << " Key =" << it1.key();
                     if (it1.value().isString()) {
                         qDebug() << " Value =" << it1.value().toString() <<endl;
                     }
                     if (it1.value().isDouble()) {
                         qDebug() << " Value =" << it1.value().toInt() <<endl;
                     }
                 }
              }else {
                 qDebug() << " Key =" << it.key() << " Value =" << it.value() <<endl;
                 if (it.value().isString()){
                     qDebug() << " Value = " << it.value().toString() <<endl;
                 }
                 if (it.value().isDouble()){
                     qDebug() << " Value = " << it.value().toInt() <<endl;
                 }
              }
          }
      
          return a.exec();
      

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      JKSHJ S 2 Replies Last reply
      2
      • N Naresh

        I am trying to parse json object in QT from an api. However when i try the codes written below i cannot parse the object. I simply want to get the information stored in those parameters. Please help me thank you !!!

        API is

        {
            "error": {
                "errorcode": 0,
                "errorstring": ""
            },
            "login": 1,
            "logintoken": "209b06aa7f059673db393ff7a731af1847344903b9733f026cc3a6f7a5b797b3"
        }
        

        The Codes are

         QNetworkReply* reply = manager->post(ava_request,postData);
        loop.exec();
        
        QString response = (QString)reply->readAll();
        qDebug() << response;
        
        QJsonDocument temp = QJsonDocument::fromJson(response.toUtf8());
        
         QJsonObject jsonObj = temp.object();
         qDebug() <<"error"<< jsonObj["error"].toString();
         qDebug() <<"login"<< jsonObj["login"].toString();
         qDebug() << "logintoken"<<jsonObj["logintoken"].toString();
        

        the response string looks

        and the output is

        D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:45 (QString NetworkConnection::Connect(QString, QString)): "<br><br>NO OF ROWDATA: 1{\"error\":{\"errorcode\":0,\"errorstring\":\"\"},\"login\":1,\"logintoken\":\"4daaf6b3dd5a26a2ad2c436e564bfa4d6c439ce4d0d6cd66705a8bdadddddaa0\"}"
        D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:50 (QString NetworkConnection::Connect(QString, QString)): error ""
        D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:51 (QString NetworkConnection::Connect(QString, QString)): login ""
        D/libAndroisShop.so(21944): ../AndroisShop/networkconnection.cpp:52 (QString NetworkConnection::Connect(QString, QString)): logintoken ""
        
        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        @Naresh said in Qt Parsing Json Object:

        and the output is

        ...

        "<br><br>NO OF ROWDATA: 1{\"error\":{\"errorcode\":0,\"errorstring\":\"\"},\"login\":1,\"logintoken\":\"4daaf6b3dd5a26a2ad2c436e564bfa4d6c439ce4d0d6cd66705a8bdadddddaa0\"}"

        This is not a valid JSON document, so you cannot pass it into QJsonDocument::fromJson().

        You must first remove the non-JSON string in front: "<br><br>NO OF ROWDATA: 1"

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        1 Reply Last reply
        7
        • dheerendraD dheerendra

          Your JSON is mix of array & objects. You need split them properly. I have given the code here. It is not optimised.

          int main(int argc, char *argv[])
          {
              QApplication a(argc, argv);
              MyWidget w;
              w.show();
          
              QString json = "{\"error\":{\"errorcode\":0,\"errorstring\":\"\"},\"login\":1,\"logintoken\":\"4daaf6b3dd5a26a2ad2c436e564bfa4d6c439ce4d0d6cd66705a8bdadddddaa0\"}";
          
              QJsonDocument temp = QJsonDocument::fromJson(json.toUtf8());
              if (temp.isArray()){
                  qDebug() << " It is an array" <<endl;
              }
              if (temp.isObject()){
                  qDebug() << " It is an Object" <<endl;
              }
          
              QJsonArray arr  = temp.array();
              qDebug() << " Array size = "  << arr.size() <<endl;
          
              QJsonObject obj  = temp.object();
              qDebug() << " Object size = "  << obj.size() <<endl;
              qDebug() << " Object size = "  << obj.keys() <<endl;
              QJsonObject::iterator it;
              for(it=obj.begin();it!=obj.end();it++){
                  if(it.value().isObject()){
                     QJsonObject o1 = it.value().toObject();
                     qDebug() << " it is JSON Object" <<endl;
                     qDebug() << " Key = " << it.key() << " Val =" << it.value() <<endl;
                     QJsonObject::iterator it1 = o1.begin();
                     for(it1=o1.begin();it1!=o1.end();it1++){
                         qDebug() << " Key =" << it1.key();
                         if (it1.value().isString()) {
                             qDebug() << " Value =" << it1.value().toString() <<endl;
                         }
                         if (it1.value().isDouble()) {
                             qDebug() << " Value =" << it1.value().toInt() <<endl;
                         }
                     }
                  }else {
                     qDebug() << " Key =" << it.key() << " Value =" << it.value() <<endl;
                     if (it.value().isString()){
                         qDebug() << " Value = " << it.value().toString() <<endl;
                     }
                     if (it.value().isDouble()){
                         qDebug() << " Value = " << it.value().toInt() <<endl;
                     }
                  }
              }
          
              return a.exec();
          
          JKSHJ Offline
          JKSHJ Offline
          JKSH
          Moderators
          wrote on last edited by
          #4

          @dheerendra said in Qt Parsing Json Object:

          Your JSON is mix of array & objects.

          There are no arrays in the @Naresh's example

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          2
          • dheerendraD Offline
            dheerendraD Offline
            dheerendra
            Qt Champions 2022
            wrote on last edited by
            #5

            @JKSH You are right. No arrays.

            Dheerendra
            @Community Service
            Certified Qt Specialist
            http://www.pthinks.com

            1 Reply Last reply
            0
            • anas yousefA Offline
              anas yousefA Offline
              anas yousef
              wrote on last edited by
              #6
              QJsonDocument doc = QJsonDocument::fromJson(response);
              QJsonObject JON = doc.object();
              QString error_code = JON.value("error").toObject().value("errorcode").toString();
              QString error_string = JON.value("error").toObject().value("errorcode").toString();
              int login = JON.value("login").toInt();
              QString login_token = JON.value("logintoken").toString();
              

              <a href="https://t.me/Lil_Nickel">Telegram</a>

              1 Reply Last reply
              0
              • dheerendraD dheerendra

                Your JSON is mix of array & objects. You need split them properly. I have given the code here. It is not optimised.

                int main(int argc, char *argv[])
                {
                    QApplication a(argc, argv);
                    MyWidget w;
                    w.show();
                
                    QString json = "{\"error\":{\"errorcode\":0,\"errorstring\":\"\"},\"login\":1,\"logintoken\":\"4daaf6b3dd5a26a2ad2c436e564bfa4d6c439ce4d0d6cd66705a8bdadddddaa0\"}";
                
                    QJsonDocument temp = QJsonDocument::fromJson(json.toUtf8());
                    if (temp.isArray()){
                        qDebug() << " It is an array" <<endl;
                    }
                    if (temp.isObject()){
                        qDebug() << " It is an Object" <<endl;
                    }
                
                    QJsonArray arr  = temp.array();
                    qDebug() << " Array size = "  << arr.size() <<endl;
                
                    QJsonObject obj  = temp.object();
                    qDebug() << " Object size = "  << obj.size() <<endl;
                    qDebug() << " Object size = "  << obj.keys() <<endl;
                    QJsonObject::iterator it;
                    for(it=obj.begin();it!=obj.end();it++){
                        if(it.value().isObject()){
                           QJsonObject o1 = it.value().toObject();
                           qDebug() << " it is JSON Object" <<endl;
                           qDebug() << " Key = " << it.key() << " Val =" << it.value() <<endl;
                           QJsonObject::iterator it1 = o1.begin();
                           for(it1=o1.begin();it1!=o1.end();it1++){
                               qDebug() << " Key =" << it1.key();
                               if (it1.value().isString()) {
                                   qDebug() << " Value =" << it1.value().toString() <<endl;
                               }
                               if (it1.value().isDouble()) {
                                   qDebug() << " Value =" << it1.value().toInt() <<endl;
                               }
                           }
                        }else {
                           qDebug() << " Key =" << it.key() << " Value =" << it.value() <<endl;
                           if (it.value().isString()){
                               qDebug() << " Value = " << it.value().toString() <<endl;
                           }
                           if (it.value().isDouble()){
                               qDebug() << " Value = " << it.value().toInt() <<endl;
                           }
                        }
                    }
                
                    return a.exec();
                
                S Offline
                S Offline
                Stephen28
                wrote on last edited by
                #7

                @dheerendra Hi, I am new to Qt and Learning Json Parsing.
                To Parse this https://reqres.in/api/users/2 ,i've used your code sample. But it didn't worked. Help me solve this

                JonBJ 1 Reply Last reply
                0
                • S Stephen28

                  @dheerendra Hi, I am new to Qt and Learning Json Parsing.
                  To Parse this https://reqres.in/api/users/2 ,i've used your code sample. But it didn't worked. Help me solve this

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

                  @Stephen28
                  Hello and welcome.

                  This is an old thread. I suggest you open your own new topic. Paste in the

                  {"data":{"id":2,"email":"janet.weaver@reqres.in","first_name":"Janet","last_name":"Weaver","avatar":"https://reqres.in/img/faces/2-image.jpg"},"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}
                  

                  you are trying to parse. Show how far you got, e.g. does

                  QString input = R"(
                  {"data":{"id":2,"email":"janet.weaver@reqres.in","first_name":"Janet","last_name":"Weaver","avatar":"https://reqres.in/img/faces/2-image.jpg"},"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}
                  )";
                  QJsonDocument doc = QJsonDocument::fromJson(input);
                  QJsonObject JON = doc.object();
                  

                  at least work to get the top-level object?

                  S 1 Reply Last reply
                  2
                  • JonBJ JonB

                    @Stephen28
                    Hello and welcome.

                    This is an old thread. I suggest you open your own new topic. Paste in the

                    {"data":{"id":2,"email":"janet.weaver@reqres.in","first_name":"Janet","last_name":"Weaver","avatar":"https://reqres.in/img/faces/2-image.jpg"},"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}
                    

                    you are trying to parse. Show how far you got, e.g. does

                    QString input = R"(
                    {"data":{"id":2,"email":"janet.weaver@reqres.in","first_name":"Janet","last_name":"Weaver","avatar":"https://reqres.in/img/faces/2-image.jpg"},"support":{"url":"https://reqres.in/#support-heading","text":"To keep ReqRes free, contributions towards server costs are appreciated!"}}
                    )";
                    QJsonDocument doc = QJsonDocument::fromJson(input);
                    QJsonObject JON = doc.object();
                    

                    at least work to get the top-level object?

                    S Offline
                    S Offline
                    Stephen28
                    wrote on last edited by
                    #9

                    @JonB said in Qt Parsing Json Object:

                    thread
                    Yes I tried this. And Worked .Thanks

                    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