我正在编写一些测试功能,这是我第一次使用Qt,并试图了解开发最终项目所需的一些知识。功能如下:

#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");
    }

}

如果是测试json,则以数组形式答复,例如:
[1,2]

一切正常,而是测试像这样的对象:
{"firstValue":1,"secondValue":2}

我得到以下错误编译:
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();

为什么不将我的测试json文档转换为对象?

提前致谢,
詹姆士

最佳答案

您忘记包括QJsonObject:

#include <QJsonDocument>
#include <QJsonValue>
#include <QJsonArray>
#include <QJsonObject>

关于json - 使用QJsonObject读取JSON,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23128904/

10-10 11:40