我目前正在开展涉及我使用The Open Movie Database API的学校项目。我觉得我的请求有效,但我似乎无法将标题,年份等信息显示在UI甚至控制台中。我试着在Qt中寻找关于JSON的教程,但我找不到任何有用的东西。任何帮助都会很棒!
我要拉什么:
void MainWindow::sendRequest(){
QEventLoop eventLoop;
QNetworkAccessManager manager;
QObject::connect(&manager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
QNetworkRequest req(QUrl(QString("http://www.omdbapi.com/?t=Scarface")));
QNetworkReply *reply = manager.get(req);
eventLoop.exec();
}
答案 0 :(得分:0)
要解码数据,您可以使用QJsonDocument,QJsonObject,QJsonArray:
#include <QEventLoop>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
void MainWindow::sendRequest()
{
QEventLoop eventLoop;
QNetworkAccessManager manager;
connect(&manager, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
QNetworkRequest req(QUrl("http://www.omdbapi.com/?t=Scarface"));
QNetworkReply *reply = manager.get(req);
eventLoop.exec();
QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
if(document.isObject()){
QJsonObject valuesO = document.object();
for(auto key: valuesO.keys()){
qDebug()<<key<<" : "<<valuesO[key].toString();
}
// access by particular key
//qDebug() << valuesO["Actors"].toString();
}
}
输出:
"Actors" : "Al Pacino, Steven Bauer, Michelle Pfeiffer, Mary Elizabeth Mastrantonio"
"Awards" : "Nominated for 3 Golden Globes. Another 4 nominations."
"BoxOffice" : "$656,161.00"
"Country" : "USA"
"DVD" : "30 Sep 2003"
"Director" : "Brian De Palma"
"Genre" : "Crime, Drama"
"Language" : "English, Spanish"
"Metascore" : "65"
"Plot" : "In Miami in 1980, a determined Cuban immigrant takes over a drug cartel and succumbs to greed."
"Poster" : "https://images-na.ssl-images-amazon.com/images/M/MV5BNjdjNGQ4NDEtNTEwYS00MTgxLTliYzQtYzE2ZDRiZjFhZmNlXkEyXkFqcGdeQXVyNjU0OTQ0OTY@._V1_SX300.jpg"
"Production" : "Universal Films"
"Rated" : "R"
"Ratings" : ""
"Released" : "09 Dec 1983"
"Response" : "True"
"Runtime" : "170 min"
"Title" : "Scarface"
"Type" : "movie"
"Website" : "N/A"
"Writer" : "Oliver Stone (screenplay)"
"Year" : "1983"
"imdbID" : "tt0086250"
"imdbRating" : "8.3"
"imdbVotes" : "572,774"