[英]Qt: How to pull a JSON request from OMDb API
I'm currently working on a school project that involves me using The Open Movie Database API. 我目前正在使用“开放电影数据库API”参与一项涉及我的学校项目。 I feel like I have the request working, but I can't seem to get the information like the Title, Year, etc to appear in the UI or even the console.
我感觉我的请求正在运行,但是我似乎无法获得标题,年份等信息出现在UI甚至控制台中。 I tried looking for tutorials about JSON in Qt but I can't find any that are useful.
我尝试在Qt中寻找有关JSON的教程,但找不到任何有用的教程。 Any help would great!
任何帮助都会很棒!
What I have to pull: 我要拉的是:
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();
}
To decode the data you can use QJsonDocument, QJsonObject, QJsonArray: 要解码数据,可以使用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();
}
}
Output: 输出:
"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"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.