Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read JSON with QJsonObject

Tags:

json

object

qt

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

like image 540
James Avatar asked Apr 17 '14 09:04

James


People also ask

Does Qt support JSON?

Qt provides support for dealing with JSON data. JSON is a format to encode object data derived from Javascript, but now widely used as a data exchange format on the internet. The JSON support in Qt provides an easy to use C++ API to parse, modify and save JSON data.

How do I add to a JSON file in Qt?

You should not append anything to the end of the json file because it is not valid json format to add others things outside of the root object. You should load it in a proper QJsonDocument , edit it as you need and write it back to the file.


1 Answers

You forget to include QJsonObject:

#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>
like image 76
Arpegius Avatar answered Sep 28 '22 12:09

Arpegius