Parse JSON response. Get title and year.
This commit is contained in:
parent
26b3c90e22
commit
1147218e0c
32
qmoviedb.cpp
32
qmoviedb.cpp
@ -1,6 +1,9 @@
|
|||||||
#include "qmoviedb.h"
|
#include "qmoviedb.h"
|
||||||
#include "QNetworkReply"
|
#include "QNetworkReply"
|
||||||
#include "QDebug"
|
#include "QDebug"
|
||||||
|
#include "QJsonDocument"
|
||||||
|
#include "QJsonObject"
|
||||||
|
#include "QJsonArray"
|
||||||
|
|
||||||
QMovieDB::QMovieDB(QString apiKey, QObject *parent)
|
QMovieDB::QMovieDB(QString apiKey, QObject *parent)
|
||||||
: QObject{parent}, netMan(new QNetworkAccessManager(this)) {
|
: QObject{parent}, netMan(new QNetworkAccessManager(this)) {
|
||||||
@ -23,7 +26,30 @@ void QMovieDB::searchMovieTitle(const QString title)
|
|||||||
netMan->get(req);
|
netMan->get(req);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QMovieDB::receiveReply(QNetworkReply *reply)
|
void QMovieDB::receiveReply(QNetworkReply *reply) {
|
||||||
{
|
int responseStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||||
qInfo() << reply->readAll();
|
|
||||||
|
if (responseStatus == 200) {
|
||||||
|
parseResults(QJsonDocument::fromJson(reply->readAll()).object());
|
||||||
|
} else {
|
||||||
|
qInfo() << "Invalid Response. Status: " << responseStatus;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void QMovieDB::parseResults(QJsonObject json) {
|
||||||
|
qInfo() << "Total Results: " << json.value("total_results").toInt();
|
||||||
|
|
||||||
|
QJsonValue results = json.value("results");
|
||||||
|
QString releaseYear;
|
||||||
|
QString titleYear;
|
||||||
|
|
||||||
|
if (results.isArray()) {
|
||||||
|
releaseYear = results.toArray().first().toObject().value("release_date").toString();
|
||||||
|
releaseYear = "(" + releaseYear.left(releaseYear.indexOf("-")) + ")";
|
||||||
|
titleYear = results.toArray().first().toObject().value("title").toString() + " " + releaseYear;
|
||||||
|
} else {
|
||||||
|
//If single result
|
||||||
|
}
|
||||||
|
|
||||||
|
qInfo() << titleYear;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QNetworkAccessManager>
|
#include <QNetworkAccessManager>
|
||||||
|
#include <QJsonObject>
|
||||||
|
|
||||||
class QMovieDB : public QObject
|
class QMovieDB : public QObject
|
||||||
{
|
{
|
||||||
@ -13,11 +14,13 @@ public:
|
|||||||
QNetworkAccessManager *netMan;
|
QNetworkAccessManager *netMan;
|
||||||
|
|
||||||
void searchMovieTitle(const QString title);
|
void searchMovieTitle(const QString title);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString movieDbApiKey;
|
QString movieDbApiKey;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void receiveReply(QNetworkReply *reply);
|
void receiveReply(QNetworkReply *reply);
|
||||||
|
void parseResults(QJsonObject json);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user