Can't get valid json object



  • I tried to parse json and get json object.
    But I can't get valid json object.

    Code

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QTcpSocket>
    #include <QUrl>
    #include <QString>
    
    #include <QNetworkAccessManager>
    
    #include <QJsonObject>
    #include <QJsonDocument>
    #include <QJsonParseError>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        /*
        QTcpSocket socket;
        //QUrl url("https://mstdn.jp/api/v1/timelines/public");
        //QString hostName = "mstdn.jp";
        QUrl url("https://teratail.com/questions/73911");
        QString hostName = "teratail.com";
        socket.connectToHost(hostName, 80);
        socket.write("GET " + url.toEncoded(QUrl::RemoveScheme | QUrl::RemoveAuthority) + " HTTP/1.1\r\nHost: " + hostName.toUtf8() + "\r\n\r\n");
        socket.waitForReadyRead(1000000);
        QString data = QString(socket.readAll());
        ui->textEdit->setText(data);
        socket.close();
        */
    
        manager = new QNetworkAccessManager(this);
        connect(manager, SIGNAL(finished(QNetworkReply*)),
                this, SLOT(replyFinished(QNetworkReply*)));
    
        //manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::replyFinished(QNetworkReply *reply)
    {
        qDebug() << "replyFinished() called.";
        if (reply->error() == QNetworkReply::NoError) {
            QByteArray byteArray = reply->readAll();
            //ui->textEdit->setPlainText(byteArray);   //####if not comment out, json text is showed well.####
    
            QJsonParseError error;
            QJsonDocument jsonDocument = QJsonDocument::fromJson(byteArray, &error);
            QJsonObject jsonObject = jsonDocument.object();
            qDebug() << error.errorString();
    
            qDebug() << jsonObject.empty();
    
            QStringList keys = jsonObject.keys();
            qDebug() << keys;
            foreach (QString key, keys) {
                qDebug() << "key =" << key;
                QString value = jsonObject.value(key).toString();
                qDebug() << "value =" << value;
            }
        }
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        /*
        QNetworkRequest request(QUrl("https://mstdn.jp/api/v1/timelines/public"));
        request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
        QByteArray par = "";
        manager->post(request, par);
        */
    
        manager->get(QNetworkRequest(QUrl("https://mstdn.jp/api/v1/timelines/public")));
        //manager->get(QNetworkRequest(QUrl("http://memotiyou.blogspot.jp/2012/02/qt-c-qnetworkaccessmanager_4306.html")));
    }
    
    

    OUTPUT

    replyFinished() called.
    "no error occurred"
    true
    ()
    


  • Hint: add

    qDebug() << "Is Object? " << jsonDocument.isObject();
    qDebug() << "Is Array? " << jsonDocument.isArray();
    

    before QJsonObject jsonObject = jsonDocument.object();



  • It went well!
    Thank you!

    CODE

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    
    #include <QTcpSocket>
    #include <QUrl>
    #include <QString>
    
    #include <QNetworkAccessManager>
    
    #include <QJsonObject>
    #include <QJsonDocument>
    #include <QJsonParseError>
    #include <QJsonArray>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        /*
        QTcpSocket socket;
        //QUrl url("https://mstdn.jp/api/v1/timelines/public");
        //QString hostName = "mstdn.jp";
        QUrl url("https://teratail.com/questions/73911");
        QString hostName = "teratail.com";
        socket.connectToHost(hostName, 80);
        socket.write("GET " + url.toEncoded(QUrl::RemoveScheme | QUrl::RemoveAuthority) + " HTTP/1.1\r\nHost: " + hostName.toUtf8() + "\r\n\r\n");
        socket.waitForReadyRead(1000000);
        QString data = QString(socket.readAll());
        ui->textEdit->setText(data);
        socket.close();
        */
    
        manager = new QNetworkAccessManager(this);
        connect(manager, SIGNAL(finished(QNetworkReply*)),
                this, SLOT(replyFinished(QNetworkReply*)));
    
        //manager->get(QNetworkRequest(QUrl("http://qt-project.org")));
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::replyFinished(QNetworkReply *reply)
    {
        qDebug() << "replyFinished() called.";
        if (reply->error() == QNetworkReply::NoError) {
            QByteArray byteArray = reply->readAll();
            //ui->textEdit->setPlainText(byteArray);
    
            QJsonParseError error;
            QJsonDocument jsonDocument = QJsonDocument::fromJson(byteArray, &error);
    
            qDebug() << "Is Object? " << jsonDocument.isObject();
            qDebug() << "Is Array? " << jsonDocument.isArray();
    
            //qDebug() << jsonDocument.;
            QJsonArray jsonArray = jsonDocument.array();
            qDebug() << jsonArray.size();
            QStringList keys = jsonArray[0].toObject().keys();
            qDebug() << keys;
            foreach (QString key, keys) {
                qDebug() << "key =" << key;
                QString value = jsonArray[0].toObject().value(key).toString();
                qDebug() << "value =" << value;
            }
    
            QJsonObject jsonObject = jsonDocument.object();
            qDebug() << error.errorString();
    
            //qDebug() << jsonObject.empty();
    
            /*
            QStringList keys = jsonObject.keys();
            qDebug() << keys;
            foreach (QString key, keys) {
                qDebug() << "key =" << key;
                QString value = jsonObject.value(key).toString();
                qDebug() << "value =" << value;
            }
            */
        }
    }
    
    void MainWindow::on_pushButton_clicked()
    {
        /*
        QNetworkRequest request(QUrl("https://mstdn.jp/api/v1/timelines/public"));
        request.setRawHeader("Content-Type", "application/x-www-form-urlencoded");
        QByteArray par = "";
        manager->post(request, par);
        */
    
        manager->get(QNetworkRequest(QUrl("https://mstdn.jp/api/v1/timelines/public")));
        //manager->get(QNetworkRequest(QUrl("http://memotiyou.blogspot.jp/2012/02/qt-c-qnetworkaccessmanager_4306.html")));
    }
    
    

Log in to reply