Qt 6: Trouble Parsing Json, object within an object
-
wrote on 27 Nov 2022, 21:31 last edited by
Hello all, im am trying to create a mashup application that makes a request to an API that gives me a Json and parse information from it. The API I am using is from tv maze (https://api.tvmaze.com/search/shows?q=) where the name of the tv show goes after the "=" sign and it returns a list of shows.
I am trying to make it where this application will let me search the tv show and return the name of the show, rating, and image of the show, and if there's multiple results it will display them as well.
I have gotten the the json to display in my code but I am having trouble trying to parse the Json.
In my code below I am trying to get the value from "name" in https://api.tvmaze.com/search/shows?q=arcane so I can store the value of the name of the show but with my code below it just returns an empty string.
Am I misunderstanding something, this is my first time attempting to use APIs and working with Jsons. Thank you!
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QNetworkAccessManager *manager = new QNetworkAccessManager(this); QString url = "https://api.tvmaze.com/search/shows?q=" + show_in; QNetworkRequest request(url); connect(manager,SIGNAL(finished(QNetworkReply*)), this,SLOT(managerFinished(QNetworkReply*))); manager->get(request); // The get request is made here } void MainWindow::managerFinished(QNetworkReply *reply) { QByteArray *myByteArray = new QByteArray(); myByteArray->append(reply->readAll()); QJsonDocument myJson = QJsonDocument::fromJson(*myByteArray); qDebug() << myJson; // Now we parse the json data QJsonObject myObj = myJson.object(); QJsonValue myValue = myObj.value("show"); QJsonObject nshow = myValue.toObject(); QJsonValue val1 = nshow.value("name"); QString nameS = val1.toString(); qDebug() << "show: " << nameS; } MainWindow::~MainWindow() { delete ui; }
-
Hello all, im am trying to create a mashup application that makes a request to an API that gives me a Json and parse information from it. The API I am using is from tv maze (https://api.tvmaze.com/search/shows?q=) where the name of the tv show goes after the "=" sign and it returns a list of shows.
I am trying to make it where this application will let me search the tv show and return the name of the show, rating, and image of the show, and if there's multiple results it will display them as well.
I have gotten the the json to display in my code but I am having trouble trying to parse the Json.
In my code below I am trying to get the value from "name" in https://api.tvmaze.com/search/shows?q=arcane so I can store the value of the name of the show but with my code below it just returns an empty string.
Am I misunderstanding something, this is my first time attempting to use APIs and working with Jsons. Thank you!
#include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); QNetworkAccessManager *manager = new QNetworkAccessManager(this); QString url = "https://api.tvmaze.com/search/shows?q=" + show_in; QNetworkRequest request(url); connect(manager,SIGNAL(finished(QNetworkReply*)), this,SLOT(managerFinished(QNetworkReply*))); manager->get(request); // The get request is made here } void MainWindow::managerFinished(QNetworkReply *reply) { QByteArray *myByteArray = new QByteArray(); myByteArray->append(reply->readAll()); QJsonDocument myJson = QJsonDocument::fromJson(*myByteArray); qDebug() << myJson; // Now we parse the json data QJsonObject myObj = myJson.object(); QJsonValue myValue = myObj.value("show"); QJsonObject nshow = myValue.toObject(); QJsonValue val1 = nshow.value("name"); QString nameS = val1.toString(); qDebug() << "show: " << nameS; } MainWindow::~MainWindow() { delete ui; }
wrote on 27 Nov 2022, 22:04 last edited by@AllLuckNoSkill
The root element is an array not an object. -
@AllLuckNoSkill
The root element is an array not an object.wrote on 27 Nov 2022, 22:46 last edited by@mpergand how would i read it in as an object instead? Or is that still wrong.
-
@mpergand how would i read it in as an object instead? Or is that still wrong.
wrote on 27 Nov 2022, 22:53 last edited by@AllLuckNoSkill
Start by checking the return results at each stage of your parsing.QJsonValue
has variousbool is...()
methods.type()
tells you what is actually there, like an object or an array. OrtoObject()
returns an emptyQJsonObject
if it's not an object, ortoArray()
an emptyQJsonArray
if it's not one, etc. Use these to check the assumptions your code is making. -
@mpergand how would i read it in as an object instead? Or is that still wrong.
wrote on 28 Nov 2022, 00:04 last edited byQJsonArray array=myJson.array(); for(int i=0; i<array.count(); i++) { QJsonObject o=array.at(i).toObject(); QJsonObject show=o.value("show").toObject(); QString name=show.value("name").toString(); qDebug()<<name; }
"Arcane"
"Archie"
"Arcane: Bridging the Rift"
"Cane"
"The Archie Show"
"Archie Bunker's Place"
....
1/5