10

I'm writing a couple test functions as it's my first time with Qt and trying to understand the bits I need to develop my end project. Here are the functions:

#include "money.h"
#include "ui_money.h"
#include <QNetworkAccessManager>
#include <QUrl>
#include <QNetworkRequest>
#include <QString>
#include <QJsonArray>
#include <QJsonDocument>



Money::Money(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::Money)
{
    ui->setupUi(this);
}

Money::~Money()
{
    delete ui;
}

void Money::on_getJsonData_clicked()
{
    QNetworkAccessManager *manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(replyFinished(QNetworkReply*)));

    manager->get(QNetworkRequest(QUrl("http://scarjamoney.no-ip.biz")));


}

void Money::replyFinished(QNetworkReply* Reply)
{
    QString string = Reply->readAll();
    QJsonDocument document = QJsonDocument::fromJson(string.toUtf8());

    if(document.isArray()){
        QJsonArray valuesA = document.array();

        foreach (const QJsonValue write, valuesA){
            //ui->textEdit->setText("dentro");
            QString text = QString::number(write.toDouble());

            //qDebug() << "ciao" << text;

            ui->textEdit->append(text);
        }
    }
    else if(document.isObject()){
        QJsonObject valuesO = document.object();
        foreach (const QJsonValue write, valuesO){
            ui->textEdit->append("inside");
        }

        ui->textEdit->append("it's an object");
    }

}

In case of a test json reply in array form eg:

[1,2]

everything works, instead in a test for objects like:

{"firstValue":1,"secondValue":2}

I get the following error compiling:

C:\Qt\Tools\QtCreator\bin\Money\money.cpp:53: error: variable 'QJsonObject valuesO' has initializer but incomplete type
     QJsonObject valuesO = document.object();
C:\Qt\Tools\QtCreator\bin\Money\money.cpp:53: error: invalid use of incomplete type 'class QJsonObject'
     QJsonObject valuesO = document.object();

Why won't it convert my test json document into an object?

Thanks in advance, James

1 Answer 1

18

You forget to include QJsonObject:

#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
Sign up to request clarification or add additional context in comments.

1 Comment

ahhh.... sometimes so simple... could kick myself, spent about an hour looking at it aswell!! Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.