Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt 6: Trouble Parsing Json, object within an object
Forum Updated to NodeBB v4.3 + New Features

Qt 6: Trouble Parsing Json, object within an object

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 395 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AllLuckNoSkill
    wrote on 27 Nov 2022, 21:31 last edited by
    #1

    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;
    }
    
    
    
    M 1 Reply Last reply 27 Nov 2022, 22:04
    0
    • A AllLuckNoSkill
      27 Nov 2022, 21:31

      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;
      }
      
      
      
      M Offline
      M Offline
      mpergand
      wrote on 27 Nov 2022, 22:04 last edited by
      #2

      @AllLuckNoSkill
      The root element is an array not an object.

      A 1 Reply Last reply 27 Nov 2022, 22:46
      1
      • M mpergand
        27 Nov 2022, 22:04

        @AllLuckNoSkill
        The root element is an array not an object.

        A Offline
        A Offline
        AllLuckNoSkill
        wrote on 27 Nov 2022, 22:46 last edited by
        #3

        @mpergand how would i read it in as an object instead? Or is that still wrong.

        J M 2 Replies Last reply 27 Nov 2022, 22:53
        0
        • A AllLuckNoSkill
          27 Nov 2022, 22:46

          @mpergand how would i read it in as an object instead? Or is that still wrong.

          J Offline
          J Offline
          JonB
          wrote on 27 Nov 2022, 22:53 last edited by
          #4

          @AllLuckNoSkill
          Start by checking the return results at each stage of your parsing. QJsonValue has various bool is...() methods. type() tells you what is actually there, like an object or an array. Or toObject() returns an empty QJsonObject if it's not an object, or toArray() an empty QJsonArray if it's not one, etc. Use these to check the assumptions your code is making.

          1 Reply Last reply
          1
          • A AllLuckNoSkill
            27 Nov 2022, 22:46

            @mpergand how would i read it in as an object instead? Or is that still wrong.

            M Offline
            M Offline
            mpergand
            wrote on 28 Nov 2022, 00:04 last edited by
            #5

            @AllLuckNoSkill

                QJsonArray 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 Reply Last reply
            0

            1/5

            27 Nov 2022, 21:31

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved