3
我在寫幾個測試函數,因爲這是我第一次使用Qt,並試圖瞭解我需要開發我的最終項目的位。下面是功能:閱讀與QJsonObject的JSON
#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文檔轉換爲對象?
由於提前, 詹姆斯
啊啊啊....有時那麼簡單......可以踢自己,花了大約一個小時看它藏漢! !謝謝 – James