c++qtqt5qjsonqjsonobject

Qt Json Parsing c++


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.

API is

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

The Codes are

  QUrl ava_url("http://pinundpin.de/wsdemo/atgdemoapi_v3/index.php");
QNetworkRequest ava_request(ava_url);
ava_request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");

QNetworkAccessManager *manager = new QNetworkAccessManager();
QEventLoop loop;

QObject::connect(manager,
                 SIGNAL(finished(QNetworkReply *)),
                 &loop,
                 SLOT(quit()));
QByteArray postData;
Username = "testwebser";
Passwd = "2017@QWEasdZXC";
postData.append("action=login&");
postData.append("password="+Passwd+"&");
postData.append("username="+Username);

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 ""

Postman Image


Solution

  • If you review the result of POSTMAN in raw mode you get the following:

    enter image description here

    What QNAM gets but POSTMAN has another algorithm to extract the json:

    enter image description here

    So a possible solution is to remove the unwanted element: <br><br>NO OF ROWDATA: 1, for this it takes advantage that the json must start with "{" so the superior substring is used.

    QUrl ava_url("http://pinundpin.de/wsdemo/atgdemoapi_v3/index.php");
    QNetworkRequest ava_request(ava_url);
    ava_request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
    
    QNetworkAccessManager *manager = new QNetworkAccessManager();
    QEventLoop loop;
    
    QObject::connect(manager,
                     &QNetworkAccessManager::finished,
                     &loop,
                     &QEventLoop::quit);
    QByteArray postData;
    QString Username = "testwebser";
    QString Passwd = "2017@QWEasdZXC";
    postData.append("action=login&");
    postData.append("password="+Passwd+"&");
    postData.append("username="+Username);
    
    QNetworkReply *reply = manager->post(ava_request,postData);
    loop.exec();
    QByteArray response = reply->readAll();
    QByteArray json = response.mid(response.indexOf("{"));
    QJsonDocument temp = QJsonDocument::fromJson(json);
    QJsonObject jsonObj = temp.object();
    QJsonObject errorObj = jsonObj["error"].toObject();
    qDebug()<<"error: "
           <<"errorcode: "<< errorObj["errorcode"].toInt()
           <<"errorstring: "<< errorObj["errorstring"].toString();
    qDebug() <<"login"<< jsonObj["login"].toInt();
    qDebug() << "logintoken"<<jsonObj["logintoken"].toString();
    

    Output:

    error:  errorcode:  0 errorstring:  ""
    login 1
    logintoken "cc7e46f34e0a268cecbaaeaad41b0ae2727cc7196b632574a4db16544576d012"