From ef2b0ced30063654f06bb1ff12ba6f2a86167bc9 Mon Sep 17 00:00:00 2001 From: Orangerot Date: Fri, 26 Jul 2024 19:17:16 +0200 Subject: feat(json): parse api --- CMakeLists.txt | 6 ++++++ main.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 08e7297..e683225 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,12 @@ set(CMAKE_CXX_STANDARD 17) add_executable(itat_challange_olympics main.cpp) +find_package(Qt6 REQUIRED COMPONENTS Core) +target_link_libraries(itat_challange_olympics PRIVATE Qt6::Core) + find_package(Qt6 REQUIRED COMPONENTS Quick) target_link_libraries(itat_challange_olympics PRIVATE Qt6::Quick) +find_package(Qt6 REQUIRED COMPONENTS Network) +target_link_libraries(itat_challange_olympics PRIVATE Qt6::Network) + diff --git a/main.cpp b/main.cpp index 4796716..6fc38fc 100644 --- a/main.cpp +++ b/main.cpp @@ -3,6 +3,24 @@ #include #include +// networking +#include +#include +#include +#include +#include + +// json parsing +#include +#include +#include +#include +#include + +// console output +#include +// #include + 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" <errorString(); + delete reply; + } + return app.exec(); } -- cgit v1.2.3