Title: split json response in Qt
Tags: json;rest;qt;asp.net-core
Question: 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 :
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&gt;
#include <QtCore/QUrl&gt;
#include <QtNetwork/QNetworkRequest&gt;
#include <QtNetwork/QNetworkReply&gt;
#include <QJsonDocument&gt;
#include <QJsonObject&gt;
#include <iostream&gt;
using namespace std;
#include <QVariant&gt;
int main(int argc, char *argv[])
QCoreApplication app(argc, argv);
QEventLoop eventLoop;
QNetworkAccessManager mgr;
QObject75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0onnect(&amp;mgr, SIGNAL(finished(QNetworkReply*)), &amp;eventLoop, SLOT(quit()));
QNetworkRequest req( QUrl( QString(&quot;http://localhost:63053/api/studentretrive/&quot;) ) );
QNetworkReply *reply = mgr.get(req);
eventLoop.exec();
if (reply-&gt;error() == QNetworkReply75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0NoError)
QString strReply = (QString)reply-&gt;readAll();
QJsonDocument jsonResponse = QJsonDocument75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0romJson(strReply.toUtf8());
QJsonObject jsonObj = jsonResponse.object();
qDebug()<<&quot;json response : &quot; << jsonResponse;
qDebug() << &quot;jsonObj count : &quot; << jsonObj.count();
qDebug() << &quot;name : &quot; << jsonObj[&quot;Name&quot;].toString();
delete reply;
qDebug() << &quot;Failure&quot; <<reply-&gt;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([{&quot;Age&quot;:26,&quot;Name&quot;:&quot;user1&quot;,&quot;RegistrationNumber&quot;:&quot;12345&quot;}])
jsonObj count :  0
name :  &quot;&quot;
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.

Here is the accepted answer: Query QJsonDocument to check it contains an array or an object with bool QJsonDocument75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0isArray() , and bool QJsonDocument75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0isObject() 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 &amp; value, jsonArray ) {
jsonObj  = value.toObject();
qDebug() << "name : " << jsonObj["Name"].toString();
Comment for this answer: thank you @Mohammad A Kanan .it worked. you made my day.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2019:00:09/21/19', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<json><rest><qt><asp.net-core>', 'Title': 'split json response in Qt', 'ViewCount': 197, 'id': 58038302, 'user_AboutMe': '', 'user_AccountId': 6762194.0, 'user_DisplayName': 'Naser Piltan', 'user_Reputation': 107.0, 'user_Views': 32.0, 'user_id': 5208522.0}}