Qt 是一個(gè)跨平臺(tái)C++圖形界面開(kāi)發(fā)庫(kù),利用Qt可以快速開(kāi)發(fā)跨平臺(tái)窗體應(yīng)用程序,在Qt中我們可以通過(guò)拖拽的方式將不同組件放到指定的位置,實(shí)現(xiàn)圖形化開(kāi)發(fā)極大的方便了開(kāi)發(fā)效率,本章將重點(diǎn)介紹如何運(yùn)用QJson
組件的實(shí)現(xiàn)對(duì)JSON文本的靈活解析功能。
JSON(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,它易于人閱讀和編寫(xiě),也易于機(jī)器解析和生成。該格式是基于JavaScript語(yǔ)言的一個(gè)子集,但它是一種獨(dú)立于語(yǔ)言的數(shù)據(jù)格式,因此可以在許多不同的編程語(yǔ)言中使用。
該數(shù)據(jù)是以鍵值對(duì)的形式組織的,其中鍵是字符串,值可以是字符串、數(shù)字、布爾值、數(shù)組、對(duì)象(即嵌套的鍵值對(duì)集合)或null,在Qt中默認(rèn)提供了QJson
系列類庫(kù),使用該類庫(kù)可以很方便的解析和處理JSON文檔。
1.1 解析單一鍵值對(duì)
實(shí)現(xiàn)解析根中的單一鍵值對(duì),例如解析config.json
配置文件中的blog,enable,status
等這些獨(dú)立的字段值,在解析之前需要先通過(guò)QJsonDocument::fromJson
將內(nèi)存中的字符串格式化為QJsonDocument
類型,當(dāng)有著該類型之后,則我們可以使用*.object()
將其轉(zhuǎn)換為對(duì)應(yīng)的QJsonObject
對(duì)象,在對(duì)象中我們可以調(diào)用各種方法對(duì)內(nèi)存中的JSON
數(shù)據(jù)進(jìn)行處理。
以下是關(guān)于 QJsonDocument
類的一些常用方法的說(shuō)明:
方法 | 說(shuō)明 |
---|---|
QJsonDocument() |
構(gòu)造函數(shù),創(chuàng)建一個(gè)空的 JSON 文檔。 |
QJsonDocument(const QJsonObject &object) |
通過(guò)給定的 JSON 對(duì)象構(gòu)造 JSON 文檔。 |
QJsonDocument(const QJsonArray &array) |
通過(guò)給定的 JSON 數(shù)組構(gòu)造 JSON 文檔。 |
QJsonDocument(const QJsonValue &value) |
通過(guò)給定的 JSON 值構(gòu)造 JSON 文檔。 |
QJsonDocument(const QJsonDocument &other) |
復(fù)制構(gòu)造函數(shù)。 |
QJsonDocument &operator=(const QJsonDocument &other) |
賦值運(yùn)算符。 |
bool isNull() const |
檢查文檔是否為空。 |
bool isEmpty() const |
檢查文檔是否為空,包括 JSON 數(shù)組或?qū)ο鬄榭盏那闆r。 |
QJsonObject object() const |
返回文檔中的 JSON 對(duì)象。 |
QJsonArray array() const |
返回文檔中的 JSON 數(shù)組。 |
QJsonValue value() const |
返回文檔中的 JSON 值。 |
bool isArray() const |
檢查文檔是否包含 JSON 數(shù)組。 |
bool isObject() const |
檢查文檔是否包含 JSON 對(duì)象。 |
QByteArray toBinaryData() const |
將文檔轉(zhuǎn)換為二進(jìn)制數(shù)據(jù)。 |
bool fromBinaryData(const QByteArray &data) |
從二進(jìn)制數(shù)據(jù)恢復(fù)文檔。 |
QString toJson(QJsonDocument::JsonFormat format = QJsonDocument::Compact) const |
返回 JSON 字符串表示,可以選擇格式化的方式。 |
static QJsonDocument fromJson(const QString &json, QJsonParseError *error = nullptr) |
從 JSON 字符串創(chuàng)建文檔。 |
以下是關(guān)于 QJsonObject
類的一些常用方法的說(shuō)明:
方法 | 說(shuō)明 |
---|---|
QJsonObject() |
構(gòu)造函數(shù),創(chuàng)建一個(gè)空的 JSON 對(duì)象。 |
QJsonObject(const QJsonObject &other) |
復(fù)制構(gòu)造函數(shù)。 |
QJsonObject &operator=(const QJsonObject &other) |
賦值運(yùn)算符。 |
bool isEmpty() const |
檢查對(duì)象是否為空。 |
int size() const |
返回對(duì)象中鍵值對(duì)的數(shù)量。 |
bool contains(const QString &key) const |
檢查對(duì)象中是否包含指定的鍵。 |
QStringList keys() const |
返回對(duì)象中所有鍵的列表。 |
QJsonValue value(const QString &key) const |
返回與指定鍵關(guān)聯(lián)的值。 |
void insert(const QString &key, const QJsonValue &value) |
向?qū)ο笾胁迦腈I值對(duì)。 |
QJsonObject &unite(const QJsonObject &other) |
將另一個(gè)對(duì)象的鍵值對(duì)合并到當(dāng)前對(duì)象。 |
void remove(const QString &key) |
從對(duì)象中移除指定鍵及其關(guān)聯(lián)的值。 |
QJsonValue take(const QString &key) |
移除并返回與指定鍵關(guān)聯(lián)的值。 |
void clear() |
移除對(duì)象中的所有鍵值對(duì),使其變?yōu)榭諏?duì)象。 |
QJsonDocument toDocument() const |
將對(duì)象轉(zhuǎn)換為 JSON 文檔。 |
當(dāng)需要讀取參數(shù)時(shí)只需要使用find()
方法查詢特定字段中的key值即可,按鈕on_pushButton_clicked
被點(diǎn)擊后執(zhí)行如下流程;
void MainWindow::on_pushButton_clicked()
{
// 字符串格式化為JSON
QJsonParseError err_rpt;
QJsonDocument root_document = QJsonDocument::fromJson(config.toUtf8(), &err_rpt);
if(err_rpt.error != QJsonParseError::NoError)
{
QMessageBox::information(nullptr,"提示","JSON格式錯(cuò)誤",QMessageBox::Ok);
}
// 獲取到Json字符串的根節(jié)點(diǎn)
QJsonObject root_object = root_document.object();
// 解析blog字段
QString blog = root_object.find("blog").value().toString();
//std::cout << "字段對(duì)應(yīng)的值 = > "<< blog.toStdString() << std::endl;
ui->lineEdit_blog->setText(blog);
// 解析enable字段
bool enable = root_object.find("enable").value().toBool();
//std::cout << "是否開(kāi)啟狀態(tài): " << enable << std::endl;
ui->lineEdit_enable->setText(QString::number(enable));
// 解析status字段
int status = root_object.find("status").value().toInt();
//std::cout << "狀態(tài)數(shù)值: " << status << std::endl;
ui->lineEdit_status->setText(QString::number(status));
}
運(yùn)行后點(diǎn)擊讀取數(shù)據(jù)按鈕,輸出效果如下;
1.2 解析單數(shù)組鍵值
實(shí)現(xiàn)解析簡(jiǎn)單的單一對(duì)象與單一數(shù)組結(jié)構(gòu),如配置文件中的GetDict
與GetList
既是我們需要解析的內(nèi)容,在解析時(shí)我們需要通過(guò)toVariantMap
將字符串轉(zhuǎn)換為對(duì)應(yīng)的Map容器,當(dāng)數(shù)據(jù)被轉(zhuǎn)換后則就可以通過(guò)Map[]
的方式很容易的將其提取出來(lái)。
void MainWindow::on_pushButton_2_clicked()
{
// 字符串格式化為JSON
QJsonParseError err_rpt;
QJsonDocument root_document = QJsonDocument::fromJson(config.toUtf8(), &err_rpt);
if(err_rpt.error != QJsonParseError::NoError)
{
QMessageBox::information(nullptr,"提示","JSON格式錯(cuò)誤",QMessageBox::Ok);
}
// 獲取到Json字符串的根節(jié)點(diǎn)
QJsonObject root_object = root_document.object();
// 解析單一對(duì)象
QJsonObject get_dict_ptr = root_object.find("GetDict").value().toObject();
QVariantMap map = get_dict_ptr.toVariantMap();
if(map.contains("address") && map.contains("username") && map.contains("password") && map.contains("update"))
{
QString address = map["address"].toString();
QString username = map["username"].toString();
QString password = map["password"].toString();
QString update = map["update"].toString();
std::cout
<< " 地址: " << address.toStdString()
<< " 用戶名: " << username.toStdString()
<< " 密碼: " << password.toStdString()
<< " 更新日期: " << update.toStdString()
<< std::endl;
ui->listWidget->addItem(address);
ui->listWidget->addItem(username);
ui->listWidget->addItem(password);
ui->listWidget->addItem(update);
}
// 解析單一數(shù)組
QJsonArray get_list_ptr = root_object.find("GetList").value().toArray();
for(int index=0; index < get_list_ptr.count(); index++)
{
int ref_value = get_list_ptr.at(index).toInt();
std::cout << "輸出數(shù)組元素: " << ref_value << std::endl;
ui->listWidget_2->addItem(QString::number(ref_value));
}
}
運(yùn)行后點(diǎn)擊解析數(shù)據(jù)按鈕,輸出效果如下;
1.3 解析多數(shù)組鍵值
實(shí)現(xiàn)解析字典嵌套字典或字典嵌套數(shù)組的結(jié)構(gòu),如配置文件中的ObjectInArrayJson
則是一個(gè)字典中嵌套了另外兩個(gè)字典而每個(gè)字典中的值又是一個(gè)Value
數(shù)組,而與之相對(duì)應(yīng)的ArrayJson
則是在列表中嵌套了另外一個(gè)列表,這兩中結(jié)構(gòu)的使用讀者可參照如下案例;
首先我們來(lái)看ObjectInArrayJson
是如何被解析的,我們分別準(zhǔn)備兩個(gè)ComboBox
選擇框,當(dāng)讀者點(diǎn)擊按鈕時(shí)我們通過(guò)toVariantMap
將字典轉(zhuǎn)換為一個(gè)MAP容器,并通過(guò)toJsonArray
轉(zhuǎn)換內(nèi)部的列表到JsonArray
容器內(nèi),其初始化部分如下所示;
void MainWindow::on_pushButton_3_clicked()
{
// 字符串格式化為JSON
QJsonParseError err_rpt;
QJsonDocument root_document = QJsonDocument::fromJson(config.toUtf8(), &err_rpt);
if(err_rpt.error != QJsonParseError::NoError)
{
QMessageBox::information(nullptr,"提示","JSON格式錯(cuò)誤",QMessageBox::Ok);
}
// 獲取到Json字符串的根節(jié)點(diǎn)
QJsonObject root_object = root_document.object();
// 找到Object對(duì)象
QJsonObject one_object_json = root_object.find("ObjectInArrayJson").value().toObject();
// 轉(zhuǎn)為MAP映射
QVariantMap map = one_object_json.toVariantMap();
// 尋找One鍵
QJsonArray array_one = map["One"].toJsonArray();
for(int index=0; index < array_one.count(); index++)
{
QString value = array_one.at(index).toString();
std::cout << "One => "<< value.toStdString() << std::endl;
ui->comboBox->addItem(value);
}
// 尋找Two鍵
QJsonArray array_two = map["Two"].toJsonArray();
for(int index=0; index < array_two.count(); index++)
{
QString value = array_two.at(index).toString();
std::cout << "Two => "<< value.toStdString() << std::endl;
ui->comboBox_2->addItem(value);
}
}
同理,要實(shí)現(xiàn)解析數(shù)組中的數(shù)組也可以通過(guò)該方式實(shí)現(xiàn),如配置文件中的ArrayJson
既是我們需要解析的內(nèi)容,首先我們通過(guò)isArray
判斷該節(jié)點(diǎn)是否為數(shù)組,如果是則通過(guò)toArray().at
方法以此得到不同下標(biāo)元素參數(shù),并依次循環(huán)即可,其代碼如下所示;
void MainWindow::on_pushButton_4_clicked()
{
// 字符串格式化為JSON
QJsonParseError err_rpt;
QJsonDocument root_document = QJsonDocument::fromJson(config.toUtf8(), &err_rpt);
if(err_rpt.error != QJsonParseError::NoError)
{
QMessageBox::information(nullptr,"提示","JSON格式錯(cuò)誤",QMessageBox::Ok);
}
// 獲取到Json字符串的根節(jié)點(diǎn)
QJsonObject root_object = root_document.object();
// 獲取MyJson數(shù)組
QJsonValue array_value = root_object.value("ArrayJson");
// 驗(yàn)證節(jié)點(diǎn)是否為數(shù)組
if(array_value.isArray())
{
// 得到數(shù)組個(gè)數(shù)
int array_count = array_value.toArray().count();
// 循環(huán)數(shù)組個(gè)數(shù)
for(int index=0;index <= array_count;index++)
{
QJsonValue parset = array_value.toArray().at((index));
if(parset.isArray())
{
QString address = parset.toArray().at(0).toString();
QString username = parset.toArray().at(1).toString();
QString userport = parset.toArray().at(2).toString();
std::cout
<< "地址: " << address.toStdString()
<< "用戶名: " << username.toStdString()
<< "端口號(hào): " << userport.toStdString()
<< std::endl;
ui->comboBox_3->addItem(address);
ui->comboBox_4->addItem(username);
ui->comboBox_5->addItem(userport);
}
}
}
}
運(yùn)行后點(diǎn)擊兩個(gè)初始化按鈕則可以將字典或列表中的數(shù)據(jù)依次解析到不同的ComBobox
列表框內(nèi),輸出效果如下;
1.4 解析多字典鍵值
實(shí)現(xiàn)解析字典中嵌套多個(gè)參數(shù)或字典中嵌套參數(shù)中包含列表的數(shù)據(jù)集,如配置文件中的ObjectJson
則是字典中存在多個(gè)鍵值對(duì),而ObjectArrayJson
則更進(jìn)一步在多鍵值對(duì)中增加了列表的支持,解析此類內(nèi)容只需要依次逐級(jí)拆分即可,我們來(lái)看下如何實(shí)現(xiàn)對(duì)這些鍵值的靈活提取;
首先我們來(lái)實(shí)現(xiàn)對(duì)ObjectJson
的參數(shù)解析功能,讀者可自行對(duì)比與之前1.3
中的區(qū)別,可以發(fā)現(xiàn)這兩者的差別其實(shí)不大,解析ObjectJson
完整代碼如下所示;
void MainWindow::on_pushButton_5_clicked()
{
// 字符串格式化為JSON
QJsonParseError err_rpt;
QJsonDocument root_document = QJsonDocument::fromJson(config.toUtf8(), &err_rpt);
if(err_rpt.error != QJsonParseError::NoError)
{
QMessageBox::information(nullptr,"提示","JSON格式錯(cuò)誤",QMessageBox::Ok);
}
// 獲取到Json字符串的根節(jié)點(diǎn)
QJsonObject root_object = root_document.object();
// 獲取MyJson數(shù)組
QJsonValue object_value = root_object.value("ObjectJson");
// 驗(yàn)證是否為數(shù)組
if(object_value.isArray())
{
// 獲取對(duì)象個(gè)數(shù)
int object_count = object_value.toArray().count();
// 循環(huán)個(gè)數(shù)
for(int index=0;index <= object_count;index++)
{
QJsonObject obj = object_value.toArray().at(index).toObject();
// 驗(yàn)證數(shù)組不為空
if(!obj.isEmpty())
{
QString address = obj.value("address").toString();
QString username = obj.value("username").toString();
std::cout << "地址: " << address.toStdString() << " 用戶: " << username.toStdString() << std::endl;
ui->comboBox_6->addItem(address);
ui->comboBox_7->addItem(username);
}
}
}
}
接著我們來(lái)實(shí)現(xiàn)一個(gè)更為復(fù)雜的需求,解析多字典中嵌套的數(shù)組,如配置文件中的ObjectArrayJson
則是我們需要解析的內(nèi)容,在之前解析字典部分保持與上述案例一致,唯一不同的是我們需要通過(guò)value("ulist").toArray()
獲取到對(duì)應(yīng)字典中的數(shù)組,并通過(guò)循環(huán)的方式輸出。
如下案例中,當(dāng)讀者點(diǎn)擊初始化按鈕時(shí)我們首先讓字典中的數(shù)據(jù)填充之ComboBox
列表框中,接著當(dāng)讀者點(diǎn)擊第一個(gè)列表框時(shí)我們讓其過(guò)濾出特定的內(nèi)容并賦值到第二個(gè)列表框中,以此實(shí)現(xiàn)聯(lián)動(dòng)效果,首先初始化部分如下所示;
void MainWindow::on_pushButton_6_clicked()
{
// 字符串格式化為JSON
QJsonParseError err_rpt;
QJsonDocument root_document = QJsonDocument::fromJson(config.toUtf8(), &err_rpt);
if(err_rpt.error != QJsonParseError::NoError)
{
QMessageBox::information(nullptr,"提示","JSON格式錯(cuò)誤",QMessageBox::Ok);
}
// 獲取到Json字符串的根節(jié)點(diǎn)
QJsonObject root_object = root_document.object();
// 獲取MyJson數(shù)組
QJsonValue object_value = root_object.value("ObjectArrayJson");
// 驗(yàn)證是否為數(shù)組
if(object_value.isArray())
{
// 獲取對(duì)象個(gè)數(shù)
int object_count = object_value.toArray().count();
// 循環(huán)個(gè)數(shù)
for(int index=0;index <= object_count;index++)
{
QJsonObject obj = object_value.toArray().at(index).toObject();
// 驗(yàn)證數(shù)組不為空
if(!obj.isEmpty())
{
QString uname = obj.value("uname").toString();
std::cout << "用戶名: " << uname.toStdString() << std::endl;
ui->comboBox_8->addItem(uname);
// 解析該用戶的數(shù)組
int array_count = obj.value("ulist").toArray().count();
std::cout << "數(shù)組個(gè)數(shù): "<< array_count << std::endl;
for(int index=0;index < array_count;index++)
{
QJsonValue parset = obj.value("ulist").toArray().at(index);
int val = parset.toInt();
std::cout << "Value = > "<< val << std::endl;
ui->comboBox_9->addItem(QString::number(val));
}
}
}
}
}
當(dāng)?shù)谝粋€(gè)選擇框被選中時(shí)我們觸發(fā)currentIndexChanged
信號(hào),在其中只需要判斷uname.compare(arg1)
是否相等如果相等則addItem
追加到新的列表內(nèi),運(yùn)行效果如下所示,詳細(xì)實(shí)現(xiàn)可參考附件。
1.5 解析多字典嵌套
實(shí)現(xiàn)解析多個(gè)字典嵌套或多個(gè)列表嵌套的結(jié)構(gòu),如配置文件中的NestingObjectJson
則是字典中嵌套字典,而ArrayNestingArrayJson
則是列表中嵌套列表,兩種的解析方式基本一致。
我們首先來(lái)實(shí)現(xiàn)第一種格式的解析,當(dāng)按鈕被點(diǎn)擊后,我們首先查詢uuid
字段并賦值到ComBobox
列表中,實(shí)現(xiàn)代碼如下所示;
void MainWindow::on_pushButton_7_clicked()
{
// 字符串格式化為JSON
QJsonParseError err_rpt;
QJsonDocument root_document = QJsonDocument::fromJson(config.toUtf8(), &err_rpt);
if(err_rpt.error != QJsonParseError::NoError)
{
QMessageBox::information(nullptr,"提示","JSON格式錯(cuò)誤",QMessageBox::Ok);
}
// 獲取到Json字符串的根節(jié)點(diǎn)
QJsonObject root_object = root_document.object();
// 獲取NestingObjectJson數(shù)組
QJsonValue array_value = root_object.value("NestingObjectJson");
// 驗(yàn)證節(jié)點(diǎn)是否為數(shù)組
if(array_value.isArray())
{
// 得到內(nèi)部對(duì)象個(gè)數(shù)
int count = array_value.toArray().count();
std::cout << "對(duì)象個(gè)數(shù): " << count << std::endl;
for(int index=0; index < count; index++)
{
// 得到數(shù)組中的index下標(biāo)中的對(duì)象
QJsonObject array_object = array_value.toArray().at(index).toObject();
QString uuid = array_object.value("uuid").toString();
// 追加數(shù)據(jù)
std::cout << uuid.toStdString() << std::endl;
ui->comboBox_10->addItem(uuid);
// 開(kāi)始解析basic中的數(shù)據(jù)
QJsonObject basic = array_object.value("basic").toObject();
QString lat = basic.value("lat").toString();
QString lon = basic.value("lon").toString();
std::cout << "解析basic中的lat字段: " << lat.toStdString() << std::endl;
std::cout << "解析basic中的lon字段: " << lon.toStdString()<< std::endl;
// 解析單獨(dú)字段
QString status = array_object.value("status").toString();
std::cout << "解析字段狀態(tài): " << status.toStdString() << std::endl;
}
}
}
當(dāng)ComBobox
組件中的currentIndexChanged
信號(hào)被觸發(fā)時(shí),則直接執(zhí)行對(duì)LineEdit
編輯框的賦值操作,其代碼如下所示;
void MainWindow::on_comboBox_10_currentIndexChanged(const QString &arg1)
{
// 字符串格式化為JSON
QJsonParseError err_rpt;
QJsonDocument root_document = QJsonDocument::fromJson(config.toUtf8(), &err_rpt);
if(err_rpt.error != QJsonParseError::NoError)
{
QMessageBox::information(nullptr,"提示","JSON格式錯(cuò)誤",QMessageBox::Ok);
}
// 獲取到Json字符串的根節(jié)點(diǎn)
QJsonObject root_object = root_document.object();
// 獲取NestingObjectJson數(shù)組
QJsonValue array_value = root_object.value("NestingObjectJson");
// 驗(yàn)證節(jié)點(diǎn)是否為數(shù)組
if(array_value.isArray())
{
// 得到內(nèi)部對(duì)象個(gè)數(shù)
int count = array_value.toArray().count();
std::cout << "對(duì)象個(gè)數(shù): " << count << std::endl;
for(int index=0; index < count; index++)
{
// 得到數(shù)組中的index下標(biāo)中的對(duì)象
QJsonObject array_object = array_value.toArray().at(index).toObject();
QString uuid = array_object.value("uuid").toString();
// 對(duì)比是否相等
if(uuid.compare(arg1) == 0)
{
// 開(kāi)始解析basic中的數(shù)據(jù)
QJsonObject basic = array_object.value("basic").toObject();
QString lat = basic.value("lat").toString();
QString lon = basic.value("lon").toString();
std::cout << "解析basic中的lat字段: " << lat.toStdString() << std::endl;
std::cout << "解析basic中的lon字段: " << lon.toStdString()<< std::endl;
ui->lineEdit->setText(lat);
ui->lineEdit_2->setText(lon);
// 解析單獨(dú)字段
QString status = array_object.value("status").toString();
std::cout << "解析字段狀態(tài): " << status.toStdString() << std::endl;
}
}
}
}
同理,我們也可以實(shí)現(xiàn)字典中嵌套列表結(jié)構(gòu),如配置文件中的ArrayNestingArrayJson
既我們需要解析的內(nèi)容,解析實(shí)現(xiàn)方法與上述代碼保持一致,首先當(dāng)按鈕被點(diǎn)擊后我們直接對(duì)ComBobox
組件進(jìn)行初始化,代碼如下所示;
void MainWindow::on_pushButton_8_clicked()
{
// 字符串格式化為JSON
QJsonParseError err_rpt;
QJsonDocument root_document = QJsonDocument::fromJson(config.toUtf8(), &err_rpt);
if(err_rpt.error != QJsonParseError::NoError)
{
std::cout << "json 格式錯(cuò)誤" << std::endl;
}
// 獲取到Json字符串的根節(jié)點(diǎn)
QJsonObject root_object = root_document.object();
// 獲取NestingObjectJson數(shù)組
QJsonValue array_value = root_object.value("ArrayNestingArrayJson");
// 驗(yàn)證節(jié)點(diǎn)是否為數(shù)組
if(array_value.isArray())
{
// 得到數(shù)組中的0號(hào)下標(biāo)中的對(duì)象
QJsonObject array_object = array_value.toArray().at(0).toObject();
// 解析手機(jī)號(hào)字符串
QString telephone = array_object.value("telephone").toString();
std::cout << "手機(jī)號(hào): " << telephone.toStdString() << std::endl;
ui->comboBox_11->addItem(telephone);
// 定位外層數(shù)組
QJsonArray root_array = array_object.find("path").value().toArray();
std::cout << "外層循環(huán)計(jì)數(shù): " << root_array.count() << std::endl;
for(int index=0; index < root_array.count(); index++)
{
// 定位內(nèi)層數(shù)組
QJsonArray sub_array = root_array.at(index).toArray();
std::cout << "內(nèi)層循環(huán)計(jì)數(shù): "<< sub_array.count() << std::endl;
for(int sub_count=0; sub_count < sub_array.count(); sub_count++)
{
// 每次取出最里層數(shù)組元素
float var = sub_array.toVariantList().at(sub_count).toFloat();
std::cout << "輸出元素: " << var << std::endl;
// std::cout << sub_array.toVariantList().at(0).toFloat() << std::endl;
ui->listWidget_3->addItem(QString::number(var));
}
}
}
}
運(yùn)行效果如下圖所示;文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-825001.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-825001.html
到了這里,關(guān)于C++ Qt開(kāi)發(fā):運(yùn)用QJSON模塊解析數(shù)據(jù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!