读取QJsonObject的封装[c++][Qt]

#include 
#include 
#include 
#include 
#include 
#include 
#include 

class MyJsonParser {
public:
    MyJsonParser() {}

    MyJsonParser(const QJsonValue& jsonValue, const QString& fieldName = "") {
        parseJsonValue(jsonValue, myStruct, fieldName);
    }

    MyJsonParser getNext(const QString& key) const {
        MyJsonParser newParser;
        newParser.myStruct = findStruct(key, myStruct);
        return newParser;
    }

    QString getValueOrFieldName() const {
        if (!fieldName.isEmpty()) {
            return fieldName;
        } else if (!myStruct.subMap.isEmpty()) {
            // 返回子字段名
            return myStruct.subMap.keys().first();
        } else if (!myStruct.arrayValues.isEmpty()) {
            // 表示这是一个数组
            return "0";
        } else {
            return myStruct.value;
        }
    }

private:
    struct MyStruct {
        QMap subMap;
        QString value;
        QVector arrayValues;
    };

    MyStruct myStruct;
    QString fieldName;

    void parseJsonValue(const QJsonValue& jsonValue, MyStruct& currentStruct, const QString& fieldName) {
        if (jsonValue.isObject()) {
            const QJsonObject jsonObj = jsonValue.toObject();
            parseJsonObject(jsonObj, currentStruct, fieldName);
        } else if (jsonValue.isArray()) {
            const QJsonArray jsonArray = jsonValue.toArray();
            parseJsonArray(jsonArray, currentStruct, fieldName);
        } else {
            currentStruct.value = jsonValue.toString();
            this->fieldName = fieldName;
        }
    }

    void parseJsonObject(const QJsonObject& jsonObj, MyStruct& currentStruct, const QString& fieldName) {
        Q_UNUSED(fieldName);
        for (auto it = jsonObj.begin(); it != jsonObj.end(); ++it) {
            QString key = it.key();
            QJsonValue value = it.value();

            parseJsonValue(value, currentStruct.subMap[key], key);
        }
    }

    void parseJsonArray(const QJsonArray& jsonArray, MyStruct& currentStruct, const QString& fieldName) {
        for (const QJsonValue& value : jsonArray) {
            MyStruct arrayElement;
            parseJsonValue(value, arrayElement, fieldName);
            currentStruct.arrayValues.append(arrayElement);
        }
    }

    MyStruct findStruct(const QString& key, const MyStruct& currentStruct) const {
        QStringList keys = key.split('.');

        const MyStruct* structPtr = ¤tStruct;
        for (const QString& k : keys) {
            if (k.toInt() >= 0 && k.toInt() < structPtr->arrayValues.size()) {
                structPtr = &structPtr->arrayValues[k.toInt()];
            } else {
                auto it = structPtr->subMap.find(k);
                if (it == structPtr->subMap.end()) {
                    return MyStruct();  // Key not found
                }
                structPtr = &it.value();
            }
        }

        return *structPtr;
    }
};


int main(int argc, char** argv) {
    Q_UNUSED(argc);
    Q_UNUSED(argv);
    // 假设你有包含数组的JSON数据
    QString jsonString = R"({
        "a": {
            "b": {
                "c": [
                    {"d": "Hello"},
                    {"d": "World"}
                ]
            },
            "e": "TalkIsCheap",
            "f": "ShowMeTheCode"
        },
        "g": []
    })";

    QJsonDocument jsonDoc = QJsonDocument::fromJson(jsonString.toUtf8());
    QJsonObject jsonObj = jsonDoc.object();

    // 使用解析器
    MyJsonParser parser(jsonObj);

    // 链式调用获取下一个层级信息的示例
    QString nextInfo = parser.getNext("a.e").getValueOrFieldName(); // 返回子字段名或值
    QString nextInfo1 = parser.getNext("a.f").getValueOrFieldName(); // 返回子字段名或值
    QString nextInfoArr = parser.getNext("a.b.c").getValueOrFieldName(); // 返回"0"表示一个数组
    QString nextInfo2 = parser.getNext("a.b.c").getNext("0.d").getValueOrFieldName(); // 返回数组0下标的字段d的值
    QString nextInfo3 = parser.getNext("a.b.c").getNext("1.d").getValueOrFieldName(); // 返回数组1下标的字段d的值
    QString nextInfoErr = parser.getNext("a.g").getValueOrFieldName(); // 不存在字段返回""
    QString nextInfoErr1 = parser.getNext("a.h").getValueOrFieldName(); // 不存在字段返回""

    // 打印结果
    qDebug() << "Next Info at a.e:" << nextInfo;
    qDebug() << "Next Info at a.f:" << nextInfo1;
    qDebug() << "Next Info at a.b.c:" << nextInfoArr;
    qDebug() << "Next Info at a.b.c.0.d:" << nextInfo2;
    qDebug() << "Next Info at a.b.c.1.d:" << nextInfo3;
    qDebug() << "Next Info at a.g:" << nextInfoErr;
    qDebug() << "Next Info at a.h:" << nextInfoErr1;

    return 0;
}

你可能感兴趣的:(c++,qt,开发语言)