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
Forum Updated to NodeBB v4.3 + New Features

Parsing JSON returned from WEB request

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 3.0k Views 2 Watching
  • 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
    Verhoher
    wrote on 27 May 2016, 09:44 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?

    P 1 Reply Last reply 27 May 2016, 10:05
    0
    • V Verhoher
      27 May 2016, 09:44

      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?

      P Offline
      P Offline
      p3c0
      Moderators
      wrote on 27 May 2016, 10:05 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

      V 1 Reply Last reply 27 May 2016, 11:08
      2
      • P p3c0
        27 May 2016, 10:05

        @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;
        
        V Offline
        V Offline
        Verhoher
        wrote on 27 May 2016, 11:08 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?

        P 1 Reply Last reply 27 May 2016, 11:22
        0
        • V Verhoher
          27 May 2016, 11:08

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

          P Offline
          P Offline
          p3c0
          Moderators
          wrote on 27 May 2016, 11:22 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

          V 1 Reply Last reply 27 May 2016, 11:30
          3
          • P p3c0
            27 May 2016, 11:22

            @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();
            }
            
            V Offline
            V Offline
            Verhoher
            wrote on 27 May 2016, 11:30 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 26 Sept 2016, 10:14 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 26 Sept 2016, 10:16 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