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 "QNetworkReply"
|
||||
#include "QDebug"
|
||||
#include "QJsonDocument"
|
||||
#include "QJsonObject"
|
||||
#include "QJsonArray"
|
||||
|
||||
QMovieDB::QMovieDB(QString apiKey, QObject *parent)
|
||||
: QObject{parent}, netMan(new QNetworkAccessManager(this)) {
|
||||
@ -23,7 +26,30 @@ void QMovieDB::searchMovieTitle(const QString title)
|
||||
netMan->get(req);
|
||||
}
|
||||
|
||||
void QMovieDB::receiveReply(QNetworkReply *reply)
|
||||
{
|
||||
qInfo() << reply->readAll();
|
||||
void QMovieDB::receiveReply(QNetworkReply *reply) {
|
||||
int responseStatus = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
|
||||
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 <QNetworkAccessManager>
|
||||
#include <QJsonObject>
|
||||
|
||||
class QMovieDB : public QObject
|
||||
{
|
||||
@ -13,11 +14,13 @@ public:
|
||||
QNetworkAccessManager *netMan;
|
||||
|
||||
void searchMovieTitle(const QString title);
|
||||
|
||||
private:
|
||||
QString movieDbApiKey;
|
||||
|
||||
private slots:
|
||||
void receiveReply(QNetworkReply *reply);
|
||||
void parseResults(QJsonObject json);
|
||||
|
||||
signals:
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user