Parsing JSON returned from WEB request



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


  • Moderators

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


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


  • Moderators

    @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();
    }
    


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



  • 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;
    }


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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.