国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

C++ Qt開(kāi)發(fā):運(yùn)用QJSON模塊解析數(shù)據(jù)

這篇具有很好參考價(jià)值的文章主要介紹了C++ Qt開(kāi)發(fā):運(yùn)用QJSON模塊解析數(shù)據(jù)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

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ù)按鈕,輸出效果如下;

C++ Qt開(kāi)發(fā):運(yùn)用QJSON模塊解析數(shù)據(jù)

1.2 解析單數(shù)組鍵值

實(shí)現(xiàn)解析簡(jiǎn)單的單一對(duì)象與單一數(shù)組結(jié)構(gòu),如配置文件中的GetDictGetList既是我們需要解析的內(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ù)按鈕,輸出效果如下;

C++ Qt開(kāi)發(fā):運(yùn)用QJSON模塊解析數(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),輸出效果如下;

C++ Qt開(kāi)發(fā):運(yùn)用QJSON模塊解析數(shù)據(jù)

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)可參考附件。

C++ Qt開(kāi)發(fā):運(yùn)用QJSON模塊解析數(shù)據(jù)

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)行效果如下圖所示;

C++ Qt開(kāi)發(fā):運(yùn)用QJSON模塊解析數(shù)據(jù)文章來(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)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 一個(gè)基于.NET Core構(gòu)建的簡(jiǎn)單、跨平臺(tái)、模塊化的商城系統(tǒng)

    一個(gè)基于.NET Core構(gòu)建的簡(jiǎn)單、跨平臺(tái)、模塊化的商城系統(tǒng)

    今天大姚給大家分享一個(gè)基于.NET Core構(gòu)建的簡(jiǎn)單、跨平臺(tái)、模塊化、完全開(kāi)源免費(fèi)(MIT License)的商城系統(tǒng):Module Shop。 商品:分類、品牌、單位、選項(xiàng)(銷售屬性)、屬性、屬性模板、屬性組。 銷售:訂單、物流。 內(nèi)容:首頁(yè)配置、評(píng)論、回復(fù)。 配置:國(guó)家、用戶、倉(cāng)庫(kù)

    2024年03月27日
    瀏覽(1132)
  • 15-1_Qt 5.9 C++開(kāi)發(fā)指南_Qt多媒體模塊概述

    15-1_Qt 5.9 C++開(kāi)發(fā)指南_Qt多媒體模塊概述

    多媒體功能指的主要是計(jì)算機(jī)的音頻和視頻的輸入、輸出、顯示和播放等功能,Qt 的多媒體模塊為音頻和視頻播放、錄音、攝像頭拍照和錄像等提供支持,甚至還提供數(shù)字收音機(jī)的支持。本章將介紹 Qt 多媒體模塊的功能和使用。 Qt 多媒體模塊提供了很多類,可以實(shí)現(xiàn)如下的

    2024年02月13日
    瀏覽(39)
  • 一站式解決方案:Qt 跨平臺(tái)開(kāi)發(fā)靈活可靠

    一站式解決方案:Qt 跨平臺(tái)開(kāi)發(fā)靈活可靠

    一站式解決方案:Qt 跨平臺(tái)開(kāi)發(fā)靈活可靠 Qt 是一種跨平臺(tái)開(kāi)發(fā)工具,為開(kāi)發(fā)者提供了一站式解決方案。無(wú)論您的項(xiàng)目目標(biāo)是 Windows、Linux、macOS、嵌入式系統(tǒng)還是移動(dòng)平臺(tái),Qt 都能勝任。這種跨平臺(tái)的特性不僅節(jié)省開(kāi)支,還推動(dòng)了戰(zhàn)略的快速落地。 適用范圍廣泛:Qt 可在多種

    2024年02月07日
    瀏覽(92)
  • 我想開(kāi)發(fā)一款跨平臺(tái)桌面軟件,請(qǐng)告訴我qt、electron、tauri、pyqt、flutter分別適合開(kāi)發(fā)哪些跨平臺(tái)桌面

    我想開(kāi)發(fā)一款跨平臺(tái)桌面軟件,請(qǐng)告訴我qt、electron、tauri、pyqt、flutter分別適合開(kāi)發(fā)哪些跨平臺(tái)桌面

    不同的跨平臺(tái)桌面開(kāi)發(fā)工具適用于不同的應(yīng)用場(chǎng)景和開(kāi)發(fā)者需求。以下是關(guān)于 Qt、Electron、Tauri、PyQt、Flutter 的簡(jiǎn)要說(shuō)明,以幫助你更好地選擇適合你項(xiàng)目的工具: Qt: 適用場(chǎng)景: Qt 是一個(gè)強(qiáng)大的 C++ 框架,適用于開(kāi)發(fā)需要高性能和原生外觀的桌面應(yīng)用。它具有廣泛的平臺(tái)支持

    2024年02月22日
    瀏覽(95)
  • C++庫(kù)封裝mongodb(跨平臺(tái)開(kāi)發(fā))

    目錄 1.開(kāi)發(fā)環(huán)境準(zhǔn)備 2.編譯mongo-c-driver (linux環(huán)境) ?3.編譯mongo-c-driver (windows環(huán)境)

    2024年02月11日
    瀏覽(28)
  • 【Visual Studio】使用 C++ 語(yǔ)言,配合 Qt,開(kāi)發(fā)了一個(gè)串口通信界面

    【Visual Studio】使用 C++ 語(yǔ)言,配合 Qt,開(kāi)發(fā)了一個(gè)串口通信界面

    知識(shí)不是單獨(dú)的,一定是成體系的。更多我的個(gè)人總結(jié)和相關(guān)經(jīng)驗(yàn)可查閱這個(gè)專欄:Visual Studio。 我要使用的功能比較簡(jiǎn)單,主要包含掃描串口、打開(kāi)串口、發(fā)送數(shù)據(jù)、接收數(shù)據(jù)、暫停按鈕、停止按鈕,因此接下里將圍繞這幾個(gè)功能依次更新。 我的工程項(xiàng)目名字叫 “GUI”。

    2024年02月11日
    瀏覽(20)
  • 3 天,入門 TAURI 并開(kāi)發(fā)一個(gè)跨平臺(tái) ChatGPT 客戶端

    3 天,入門 TAURI 并開(kāi)發(fā)一個(gè)跨平臺(tái) ChatGPT 客戶端

    ? TAURI 是一個(gè)使用 Rust 編寫(xiě)的程序框架,它允許我們使用 Web 技術(shù)和 Rust 語(yǔ)言構(gòu)建跨端應(yīng)用。它提供了大量特性,例如系統(tǒng)通知、網(wǎng)絡(luò)請(qǐng)求、全局快捷鍵、本地文件處理等,它們都可以在前端通過(guò) JavaScript 便捷的調(diào)用。 TAURI 應(yīng)用的后端基于 Rust,這是一種內(nèi)存安全、性能出色

    2024年02月13日
    瀏覽(22)
  • Unity開(kāi)發(fā)進(jìn)行C、C++源碼交互,支持跨平臺(tái)

    Unity開(kāi)發(fā)進(jìn)行C、C++源碼交互,支持跨平臺(tái)

    公司新項(xiàng)目,要和做C++算法的人一起合作開(kāi)發(fā),起初項(xiàng)目定于Windows平臺(tái),就看了一些C++和DLL交互的一些資料,做了一套生成DLL交互的接口,后來(lái)項(xiàng)目寫(xiě)方案由于設(shè)備又定到安卓平臺(tái),嘗試過(guò)打包之后,DLL打包不到安卓平臺(tái),試過(guò)將dll改名打AB包然后用Assembly.Load的方式,但這種

    2024年01月18日
    瀏覽(43)
  • 【Flutter跨平臺(tái)插件開(kāi)發(fā)】如何實(shí)現(xiàn)kotlin跟C++的相互調(diào)用

    在 Kotlin 中,可以使用 JNI (Java Native Interface) 來(lái)調(diào)用 C++ 代碼 調(diào)用步驟: 創(chuàng)建 C++ 文件并實(shí)現(xiàn)函數(shù)。 在 Kotlin 中聲明需要調(diào)用的 native 函數(shù)并加載 native 庫(kù)。 調(diào)用示例 Flutter 插件項(xiàng)目的例子 在 Flutter 插件中引用已有的 C++ 源碼需要以下步驟: 首先,在 Flutter 插件的 android 目錄下

    2024年01月25日
    瀏覽(100)
  • 九、Qt C++ 數(shù)據(jù)庫(kù)開(kāi)發(fā)

    九、Qt C++ 數(shù)據(jù)庫(kù)開(kāi)發(fā)

    《一、QT的前世今生》 《二、QT下載、安裝及問(wèn)題解決(windows系統(tǒng))》 《三、Qt Creator使用》???? 《四、Qt 的第一個(gè)demo-CSDN博客》 《五、帶登錄窗體的demo》 《六、新建窗體時(shí),幾種窗體的區(qū)別》? 《七、Qt 信號(hào)和槽》? 《八、Qt C++ 畢業(yè)設(shè)計(jì)》 《九、Qt C++ 數(shù)據(jù)庫(kù)開(kāi)發(fā)》 《

    2024年01月18日
    瀏覽(22)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包