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. Parsing JSON returned from WEB request
QtWS25 Last Chance

Parsing JSON returned from WEB request

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 3.0k 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.
  • VerhoherV Offline
    VerhoherV Offline
    Verhoher
    wrote on last edited by
    #1

    Hello,
    I'm trying to parse some JSON returned from a web request, code looks like:

        qDebug()<< "Got reply";
        QString data = (QString) currentReply -> readAll();
    
        qDebug()<< "Got data "<< data;
        QScriptEngine engine;
        QScriptValue result = engine.evaluate(data);
        QScriptValue property = result.property("resource");
        QScriptValueIterator it(property);
        while (it.hasNext()){
            it.next();
            QScriptValue entry = it.value();
            QString loginValue = entry.property("Login").toString();
            qDebug()<<"!!Got Login "<< loginValue;
        }
    

    The solution doesn't seem to go in to the while loop, although the JSON returned, if simply call the request is

    {
      "resource": [
        {
          "Login": "test",
          "Email": "test"
        }
      ]
    }
    

    but the one logged in qDebug()<< "Got data "<< data; looks like:

    Got data  "{\"resource\":[{\"Login\":\"test\",\"Email\":\"test\"}]}"
    

    How should I parse The returned JSON?

    p3c0P 1 Reply Last reply
    0
    • VerhoherV Verhoher

      Hello,
      I'm trying to parse some JSON returned from a web request, code looks like:

          qDebug()<< "Got reply";
          QString data = (QString) currentReply -> readAll();
      
          qDebug()<< "Got data "<< data;
          QScriptEngine engine;
          QScriptValue result = engine.evaluate(data);
          QScriptValue property = result.property("resource");
          QScriptValueIterator it(property);
          while (it.hasNext()){
              it.next();
              QScriptValue entry = it.value();
              QString loginValue = entry.property("Login").toString();
              qDebug()<<"!!Got Login "<< loginValue;
          }
      

      The solution doesn't seem to go in to the while loop, although the JSON returned, if simply call the request is

      {
        "resource": [
          {
            "Login": "test",
            "Email": "test"
          }
        ]
      }
      

      but the one logged in qDebug()<< "Got data "<< data; looks like:

      Got data  "{\"resource\":[{\"Login\":\"test\",\"Email\":\"test\"}]}"
      

      How should I parse The returned JSON?

      p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      @Verhoher To parse the JSON use QJsonDocument. As far as the ouput is concerned the printing with qDebug has changed a little bit. Try following:

      QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
      QString str = doc.toJson();
      QDebug debug = qDebug();
      debug.noquote();
      debug << str;
      

      157

      VerhoherV 1 Reply Last reply
      2
      • p3c0P p3c0

        @Verhoher To parse the JSON use QJsonDocument. As far as the ouput is concerned the printing with qDebug has changed a little bit. Try following:

        QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
        QString str = doc.toJson();
        QDebug debug = qDebug();
        debug.noquote();
        debug << str;
        
        VerhoherV Offline
        VerhoherV Offline
        Verhoher
        wrote on last edited by
        #3

        @p3c0 Sorry, seems that I just can't grasp how I would work with the JSON document.
        So I receive it as:

        {
            "resource": [
                {
                    "Email": "test",
                    "Login": "test"
                }
            ]
        }
        

        and now I guess I can use either of:

           QJsonObject jsonOb = doc.object();
            QVariant data = doc.toVariant();
        

        But I can't manage to get to the data inside(eg. with jsonOb.value("Login").toString()), could you please give an example of how I would get the login for example from the jsonOb?

        p3c0P 1 Reply Last reply
        0
        • VerhoherV Verhoher

          @p3c0 Sorry, seems that I just can't grasp how I would work with the JSON document.
          So I receive it as:

          {
              "resource": [
                  {
                      "Email": "test",
                      "Login": "test"
                  }
              ]
          }
          

          and now I guess I can use either of:

             QJsonObject jsonOb = doc.object();
              QVariant data = doc.toVariant();
          

          But I can't manage to get to the data inside(eg. with jsonOb.value("Login").toString()), could you please give an example of how I would get the login for example from the jsonOb?

          p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          @Verhoher Login is inside a JSON array so to parse it we have QJsonArray. Then you can iterate over the array and convert the individual array values to QJsonObject. Once this is done you can access the values inside like anyother array.
          For eg.:

          QJsonObject obj = doc.object();
          QJsonArray array = obj["items"].toArray();
          foreach (const QJsonValue & value, array) {
              QJsonObject obj = value.toObject();
              QString login = obj["Login"].toString();
          }
          

          157

          VerhoherV 1 Reply Last reply
          3
          • p3c0P p3c0

            @Verhoher Login is inside a JSON array so to parse it we have QJsonArray. Then you can iterate over the array and convert the individual array values to QJsonObject. Once this is done you can access the values inside like anyother array.
            For eg.:

            QJsonObject obj = doc.object();
            QJsonArray array = obj["items"].toArray();
            foreach (const QJsonValue & value, array) {
                QJsonObject obj = value.toObject();
                QString login = obj["Login"].toString();
            }
            
            VerhoherV Offline
            VerhoherV Offline
            Verhoher
            wrote on last edited by
            #5

            @p3c0 Thank you very much, this cleared up a lot.

            1 Reply Last reply
            1
            • Pradeep KumarP Offline
              Pradeep KumarP Offline
              Pradeep Kumar
              wrote on last edited by
              #6

              QByteArray data = "{"resource":[{"Login":"test","Email":"test"}]}";

              QJsonDocument document = QJsonDocument::fromJson(data);
              qDebug () << "json document :  : " <<document <<  endl;
              
              if(!document.isNull())
              {
                  if(document.isObject())
                  {
                      QJsonObject obj = document.object();
                      qDebug () << "json obj : " <<obj <<  endl;
              
                      if(!obj.isEmpty())
                      {
                          QJsonArray jsonArray = obj["resource"].toArray();
              
                          foreach (const QJsonValue & value, jsonArray) {
                              QJsonObject obj = value.toObject();
              
                              QString login = (obj["Login"].toString());
                              QString email = (obj["Email"].toString());
              
                              qDebug () << "login :" << login << endl;
                              qDebug () << "email :" << email << endl;
                          }
                      }
                      else
                      {
                          qDebug () << "Json object is  empty :" << endl;
                      }
                  }
                  else
                  {
                      qDebug () << "Json Document is not Object :" << endl;
                  }
              }
              else
              {
                  qDebug () << "Invalid Json :" << endl;
              }
              

              Pradeep Kumar
              Qt,QML Developer

              1 Reply Last reply
              3
              • Pradeep KumarP Offline
                Pradeep KumarP Offline
                Pradeep Kumar
                wrote on last edited by
                #7

                Just trying to parse got the answer, so posted the complete solution of parsing.
                Thanks for the question posted and solutions.

                Pradeep Kumar
                Qt,QML Developer

                1 Reply Last reply
                1

                • Login

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