C++?Qt開發(fā)之運(yùn)用QJSON模塊解析數(shù)據(jù)
Qt 是一個(gè)跨平臺(tái)C++圖形界面開發(fā)庫(kù),利用Qt可以快速開發(fā)跨平臺(tái)窗體應(yīng)用程序,在Qt中我們可以通過拖拽的方式將不同組件放到指定的位置,實(shí)現(xiàn)圖形化開發(fā)極大的方便了開發(fā)效率,本章將重點(diǎn)介紹如何運(yùn)用QJson組件的實(shí)現(xiàn)對(duì)JSON文本的靈活解析功能。
JSON(JavaScript Object Notation)是一種輕量級(jí)的數(shù)據(jù)交換格式,它易于人閱讀和編寫,也易于機(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 解析單一鍵值對(duì)
實(shí)現(xiàn)解析根中的單一鍵值對(duì),例如解析config.json配置文件中的blog,enable,status等這些獨(dú)立的字段值,在解析之前需要先通過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 類的一些常用方法的說明:
| 方法 | 說明 |
|---|---|
| QJsonDocument() | 構(gòu)造函數(shù),創(chuàng)建一個(gè)空的 JSON 文檔。 |
| QJsonDocument(const QJsonObject &object) | 通過給定的 JSON 對(duì)象構(gòu)造 JSON 文檔。 |
| QJsonDocument(const QJsonArray &array) | 通過給定的 JSON 數(shù)組構(gòu)造 JSON 文檔。 |
| QJsonDocument(const QJsonValue &value) | 通過給定的 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 類的一些常用方法的說明:
| 方法 | 說明 |
|---|---|
| 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 << "是否開啟狀態(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ù)按鈕,輸出效果如下;

2 解析單數(shù)組鍵值
實(shí)現(xiàn)解析簡(jiǎn)單的單一對(duì)象與單一數(shù)組結(jié)構(gòu),如配置文件中的GetDict與GetList既是我們需要解析的內(nèi)容,在解析時(shí)我們需要通過toVariantMap將字符串轉(zhuǎn)換為對(duì)應(yīng)的Map容器,當(dāng)數(shù)據(jù)被轉(zhuǎn)換后則就可以通過Map[]的方式很容易的將其提取出來。
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ù)按鈕,輸出效果如下;

3 解析多數(shù)組鍵值
實(shí)現(xiàn)解析字典嵌套字典或字典嵌套數(shù)組的結(jié)構(gòu),如配置文件中的ObjectInArrayJson則是一個(gè)字典中嵌套了另外兩個(gè)字典而每個(gè)字典中的值又是一個(gè)Value數(shù)組,而與之相對(duì)應(yīng)的ArrayJson則是在列表中嵌套了另外一個(gè)列表,這兩中結(jié)構(gòu)的使用讀者可參照如下案例;
首先我們來看ObjectInArrayJson是如何被解析的,我們分別準(zhǔn)備兩個(gè)ComboBox選擇框,當(dāng)讀者點(diǎn)擊按鈕時(shí)我們通過toVariantMap將字典轉(zhuǎn)換為一個(gè)MAP容器,并通過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ù)組也可以通過該方式實(shí)現(xiàn),如配置文件中的ArrayJson既是我們需要解析的內(nèi)容,首先我們通過isArray判斷該節(jié)點(diǎn)是否為數(shù)組,如果是則通過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),輸出效果如下;

4 解析多字典鍵值
實(shí)現(xiàn)解析字典中嵌套多個(gè)參數(shù)或字典中嵌套參數(shù)中包含列表的數(shù)據(jù)集,如配置文件中的ObjectJson則是字典中存在多個(gè)鍵值對(duì),而ObjectArrayJson則更進(jìn)一步在多鍵值對(duì)中增加了列表的支持,解析此類內(nèi)容只需要依次逐級(jí)拆分即可,我們來看下如何實(shí)現(xiàn)對(duì)這些鍵值的靈活提?。?/p>
首先我們來實(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);
}
}
}
}
接著我們來實(shí)現(xiàn)一個(gè)更為復(fù)雜的需求,解析多字典中嵌套的數(shù)組,如配置文件中的ObjectArrayJson則是我們需要解析的內(nèi)容,在之前解析字典部分保持與上述案例一致,唯一不同的是我們需要通過value("ulist").toArray()獲取到對(duì)應(yīng)字典中的數(shù)組,并通過循環(huán)的方式輸出。
如下案例中,當(dāng)讀者點(diǎn)擊初始化按鈕時(shí)我們首先讓字典中的數(shù)據(jù)填充之ComboBox列表框中,接著當(dāng)讀者點(diǎn)擊第一個(gè)列表框時(shí)我們讓其過濾出特定的內(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)可參考附件。

5 解析多字典嵌套
實(shí)現(xiàn)解析多個(gè)字典嵌套或多個(gè)列表嵌套的結(jié)構(gòu),如配置文件中的NestingObjectJson則是字典中嵌套字典,而ArrayNestingArrayJson則是列表中嵌套列表,兩種的解析方式基本一致。
我們首先來實(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);
// 開始解析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)
{
// 開始解析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)行效果如下圖所示;

以上就是C++ Qt開發(fā)之運(yùn)用QJSON模塊解析數(shù)據(jù)的詳細(xì)內(nèi)容,更多關(guān)于Qt QJSON解析數(shù)據(jù)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
利用C語(yǔ)言實(shí)現(xiàn)將格式化數(shù)據(jù)和字符串相互轉(zhuǎn)換
這篇文章主要為大家詳細(xì)介紹了2個(gè)函數(shù),分別是sprintf和sscanf,可以用來實(shí)現(xiàn)將格式化數(shù)據(jù)和字符串相互轉(zhuǎn)換,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03
C++中strcpy函數(shù)的實(shí)現(xiàn)
strncpy這個(gè)可以指定拷貝字符的長(zhǎng)度,指定源地址,目標(biāo)地址,還有需要拷貝的字符的長(zhǎng)度; strcpy只能傳入兩個(gè)參數(shù),只指定拷貝的起始地址跟目標(biāo)地址,然后整體拷貝;2015-10-10
C語(yǔ)言 常量,變量及數(shù)據(jù)詳細(xì)介紹
這篇文章主要介紹了C語(yǔ)言 常量,變量及數(shù)據(jù)詳解的相關(guān)資料,需要的朋友可以參考下2016-10-10
c語(yǔ)言函數(shù)如何求兩個(gè)數(shù)的最大值
這篇文章主要介紹了c語(yǔ)言函數(shù)如何求兩個(gè)數(shù)的最大值問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
如何判斷一個(gè)整數(shù)的二進(jìn)制中有多少個(gè)1
本篇文章是對(duì)如何判斷一個(gè)整數(shù)的二進(jìn)制中有多少個(gè)1的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
C語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)猜數(shù)字游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11
C++動(dòng)態(tài)規(guī)劃算法實(shí)現(xiàn)矩陣鏈乘法
動(dòng)態(tài)規(guī)劃算法通常用于求解具有某種最優(yōu)性質(zhì)的問題。在這類問題中,可能會(huì)有許多可行解。每一個(gè)解都對(duì)應(yīng)于一個(gè)值,我們希望找到具有最優(yōu)值的解2022-06-06

