diff options
author | Orangerot <purple@orangerot.dev> | 2024-07-26 19:17:16 +0200 |
---|---|---|
committer | Orangerot <purple@orangerot.dev> | 2024-07-26 19:17:16 +0200 |
commit | ef2b0ced30063654f06bb1ff12ba6f2a86167bc9 (patch) | |
tree | 00fd1ad80a883d5b7a2e0cb301e1141131c17711 /main.cpp | |
parent | a5ba031c9dfc830d3fe44e868c5fd26af2fbb225 (diff) |
feat(json): parse api
Diffstat (limited to 'main.cpp')
-rw-r--r-- | main.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
@@ -3,6 +3,24 @@ #include <QQmlContext> #include <QQmlComponent> +// networking +#include <QNetworkAccessManager> +#include <QNetworkRequest> +#include <QNetworkReply> +#include <QUrl> +#include <QUrlQuery> + +// json parsing +#include <QJsonValue> +#include <QJsonDocument> +#include <QJsonObject> +#include <QVariantMap> +#include <QJsonArray> + +// console output +#include <QDebug> +// #include <iostream> + int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); @@ -15,5 +33,41 @@ int main(int argc, char *argv[]) // ... delete object and objectContext when necessary + + // create custom temporary event loop on stack + QEventLoop eventLoop; + + // "quit()" the event-loop, when the network request "finished()" + QNetworkAccessManager mgr; + QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit())); + + QString endpoint { "https://sph-s-api.olympics.com/summer/schedules/api/ENG/schedule/discipline/ARC" }; + + // the HTTP request + QNetworkRequest req( (QUrl( endpoint )) ); + QNetworkReply *reply = mgr.get(req); + eventLoop.exec(); // blocks stack until "finished()" has been called + + if (reply->error() == QNetworkReply::NoError) { + //success + + QString strReply = (QString)reply->readAll(); + + //parse json + // qDebug() << "Response:" << strReply; + QJsonDocument jsonResponse = QJsonDocument::fromJson(strReply.toUtf8()); + + QJsonObject jsonObj = jsonResponse.object(); + + qDebug() << "Competitor:" << jsonObj["units"][0]["competitors"][0]["name"].toString(); + + delete reply; + } + else { + //failure + qDebug() << "Failure" <<reply->errorString(); + delete reply; + } + return app.exec(); } |