QT开发中文网

上述,doxument是否可以作为类的成员变量?方便使用。派生类是否也可以使用该类成员变量?

是的,QJsonDocument可以作为类的成员变量。将其作为类的成员变量可以使得在该类中的其他成员函数中更方便地访问和操作JSON数据。

以下是一个示例,展示如何将QJsonDocument作为类的成员变量,并且派生类也可以使用它:

示例代码

#include <QCoreApplication>
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>

class JsonHandler {
public:
    JsonHandler(const QString &filePath) {
        loadJson(filePath);
    }

    void printValue(const QString &key) {
        if (jsonDoc.isNull()) {
            qDebug() << "JSON document is null.";
            return;
        }

        if (jsonDoc.isObject()) {
            QJsonObject jsonObject = jsonDoc.object();
            if (jsonObject.contains(key)) {
                qDebug() << "Value for" << key << ":" << jsonObject[key].toString();
            } else {
                qDebug() << "Key" << key << "not found.";
            }
        }
    }

protected:
    void loadJson(const QString &filePath) {
        QFile file(filePath);
        if (!file.open(QIODevice::ReadOnly)) {
            qDebug() << "Could not open the file!";
            return;
        }

        QByteArray jsonData = file.readAll();
        file.close();

        jsonDoc = QJsonDocument::fromJson(jsonData);
    }

private:
    QJsonDocument jsonDoc;
};

class ExtendedJsonHandler : public JsonHandler {
public:
    ExtendedJsonHandler(const QString &filePath) : JsonHandler(filePath) {}

    void printArrayValues(const QString &arrayKey) {
        if (jsonDoc.isNull()) {
            qDebug() << "JSON document is null.";
            return;
        }

        if (jsonDoc.isObject()) {
            QJsonObject jsonObject = jsonDoc.object();
            if (jsonObject.contains(arrayKey) && jsonObject[arrayKey].isArray()) {
                QJsonArray jsonArray = jsonObject[arrayKey].toArray();
                for (const QJsonValue &value : jsonArray) {
                    qDebug() << value.toString();  // 根据需要进行处理
                }
            } else {
                qDebug() << "No array found at key" << arrayKey;
            }
        }
    }
};

int main(int argc, char *argv[]) {
    QCoreApplication a(argc, argv);

    ExtendedJsonHandler handler("data.json");
    
    // 打印特定键的值
    handler.printValue("key");
    
    // 打印数组的所有值
    handler.printArrayValues("arrayKey");

    return a.exec();
}

解释

  1. JsonHandler:这是一个基本类,它有一个私有成员 jsonDoc(类型为 QJsonDocument)。它在构造函数中加载 JSON 数据,并提供了方法来打印特定键的值。

  2. ExtendedJsonHandler:这个类继承自 JsonHandler,并且可以使用父类中的 jsonDoc 成员变量。它添加了一个新方法,用于打印指定数组键下的所有值。

  3. 安全性:注意,直接通过子类访问父类私有成员是不可能的,但你可以通过保护或公开的方法访问这些数据。如果你希望子类能够直接访问这个文档,你可以将其声明为 protected

这种设计允许灵活地扩展功能,同时保持代码清晰和易于维护。


内容由零声教学AI助手提供,问题来源于学员提问

目录导航
  • 示例代码
  • 解释
  • 本站部分文章来源于网络,版权归原作者所有,如有侵权请联系站长删除。
    转载请注明出处:https://qt.0voice.com/?id=12594

    分享:
    扫描分享到社交APP
    上一篇
    下一篇
    发表列表
    游客 游客
    此处应有掌声~
    评论列表

    还没有评论,快来说点什么吧~

    联系我们

    在线咨询: 点击这里给我发消息

    微信号:3007537140

    上班时间: 10:30-22:30

    关注我们
    x

    注册

    已经有帐号?