Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Parsing JSON returned from WEB request

    General and Desktop
    3
    7
    2574
    Loading More Posts
    • 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.
    • Verhoher
      Verhoher last edited by

      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?

      p3c0 1 Reply Last reply Reply Quote 0
      • p3c0
        p3c0 Moderators @Verhoher last edited by

        @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

        Verhoher 1 Reply Last reply Reply Quote 2
        • Verhoher
          Verhoher @p3c0 last edited by

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

          p3c0 1 Reply Last reply Reply Quote 0
          • p3c0
            p3c0 Moderators @Verhoher last edited by

            @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

            Verhoher 1 Reply Last reply Reply Quote 3
            • Verhoher
              Verhoher @p3c0 last edited by

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

              1 Reply Last reply Reply Quote 1
              • Pradeep Kumar
                Pradeep Kumar last edited by

                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 Reply Quote 3
                • Pradeep Kumar
                  Pradeep Kumar last edited by

                  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 Reply Quote 1
                  • First post
                    Last post