我正在编写一些测试函数,因为这是我第一次接触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文档转换成对象?提前感谢, 詹姆斯