'split json response in Qt

I have created a Restful Web Service in ASP.NET and would like to use it in my own Qt project.
I can post to and get the data from my Web Service in the SoapUi like this :

enter image description here

So it seems that my Rest Webservice works correctly.

I use this code snippet to retrieve the data from my web service in Qt5 :

main.cpp

#include <QCoreApplication>
#include <QtCore/QUrl>
#include <QtNetwork/QNetworkRequest>
#include <QtNetwork/QNetworkReply>
#include <QJsonDocument>
#include <QJsonObject>
#include <iostream>
using namespace std;
#include <QVariant>

int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);

QEventLoop eventLoop;
QNetworkAccessManager mgr;
QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
QNetworkRequest req( QUrl( QString("http://localhost:63053/api/studentretrive/") ) );
QNetworkReply *reply = mgr.get(req);
eventLoop.exec();

if (reply->error() == QNetworkReply::NoError)
{
    QString strReply = (QString)reply->readAll();
    QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8());
    QJsonObject jsonObj = jsonResponse.object();
    qDebug()<<"json response : " << jsonResponse;
    qDebug() << "jsonObj count : " << jsonObj.count();
    qDebug() << "name : " << jsonObj["Name"].toString();
    delete reply;
    
}

else
{
    qDebug() << "Failure" <<reply->errorString();
    delete reply;
}


return 0;
}

main.pro

QT -= gui
QT += network
CONFIG += c++11
CONFIG -= app_bundle
DEFINES += QT_DEPRECATED_WARNINGS
SOURCES += main.cpp

when I run the code. the output is as follows:

qt.network.ssl: QSslSocket: cannot resolve SSL_set_alpn_protos
qt.network.ssl: QSslSocket: cannot resolve SSL_CTX_set_alpn_select_cb
qt.network.ssl: QSslSocket: cannot resolve SSL_get0_alpn_selected
json response :  QJsonDocument([{"Age":26,"Name":"user1","RegistrationNumber":"12345"}])
jsonObj count :  0
name :  ""

It means that I got a response from the server, but I have no JSON object and thus can not split the server's response. I surfed the Internet and bumped into some solutions but no one worked for me. what is the deal? Should I add some more code to the .pro file or there is a mistake in the code?
Any help is appreciated.



Solution 1:[1]

Query QJsonDocument to check it contains an array or an object with bool QJsonDocument::isArray() , and bool QJsonDocument::isObject() before retrieving its content.

Depending on whether the QJsonDocument contains an object or array you can do the right retrieval , use a safe code when you don't know or not sure what the response is :

QJsonObject jsonObj;
if (jsonResponse.isObject() )
  jsonObj = jsonResponse.object();
else if (jsonResponse.isArray() ) {
 QJsonArray jsonArray = jsonResponse.array();
   foreach (const QJsonValue & value, jsonArray ) {
    jsonObj  = value.toObject();
    qDebug() << "name : " << jsonObj["Name"].toString();
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mohammad Kanan