将 json 字符串解析为 QMap

Parse json string to QMap

提问人:Jabu 提问时间:8/26/2023 更新时间:8/26/2023 访问量:39

问:

我没有弄清楚如何正确解析 JSON 字符串并将其转换为 我怀疑依赖于检查某物是否是数组或对象并递归迭代它,将每个值添加到其相应的键/父对象。QMap<QString, QString>

我这样做的方式是这样构建的:map

enter image description here

#include <QtCore/QCoreApplication>
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
    
void parseJsonObject(const QJsonObject &jsonObject, QMap<QString, QString> &map, const QString &parentKey = "")
{
    for(auto it = jsonObject.begin(); it != jsonObject.end(); ++it)
    {
        QString key = it.key();
        QJsonValue value = it.value();

        QString newKey = parentKey.isEmpty() ? key : parentKey + "." + key;

        if(value.isObject())
        {
            parseJsonObject(value.toObject(), map, newKey);
        }
        else if(value.isArray())
        {
            int index = 0;
            for(const QJsonValue &arrayValue : value.toArray())
            {
                if(arrayValue.isObject())
                {
                    parseJsonObject(arrayValue.toObject(), map, newKey + "." + QString::number(index));
                }
                else
                {
                    map.insert(newKey + "." + QString::number(index), arrayValue.toString());
                }
                index++;
            }
        }
        else
        {
            map.insert(newKey, value.toString());
        }
    }
}
    
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString jsonString = R"({"files":{"App.exe":"f97611d3214dcb5146240c89a178b04a1e8100e870ec86468324aefc480d1946","D3Dcompiler_47.dll":"954ff81e19489b12645170aac2332a3f9829b66c626c164a47f983b05ba52f6c","generic":{"qtuiotouchplugin.dll":"ed725b71f5c6828762366686605e5a22bca1e66275a9a98c8d463a65f5eff4ff","test":{"xx.txt":""}},"iconengines":{"qsvgicon.dll":"02efd2585b646685b92322f1bca2cfd34282fab4ef9e10ea542e6acdedd867a3"},"imageformats":{"qgif.dll":"f73847dee80df1a80ac4442ec7d3feb5c865d6d2d4797c038c96196a4d24bda4","qicns.dll":"5fa7b0f2667ad3855f59422822e47e623a71e5c11067151213442b43091c47d3","qico.dll":"20b74e99dca0c80d3c9c2686a14184d1219edcb7294de8467e48a98d97399a13","qjpeg.dll":"d0b0af1a2a343546dcf737c74e3f11dcd665dd14c057ce46ea4784eeba7b8137","qsvg.dll":"c3c794214a18ceed47a6d30cb8784d2c0c9c3c2e41c1b397e9f99711e8173003","qtga.dll":"1121c5a2b59cef2d0c222f93481cfd16f0fd29c20768eb2bc77d9364392ca2f5","qtiff.dll":"27fa978a9a0db9c4fdc46a87675982fd8ae8727cad04f1542bcf426990817611","qwbmp.dll":"2c3769f6310ab957ef0825bfae204972c393c275819dbcc47f1dd8d3e58f2a56","qwebp.dll":"e916f1ef2938a59d8147b7da877d9b55a7dd4586b5f47d4ea8b844d276da6c98"},"networkinformation":{"qnetworklistmanager.dll":"980d0a553572f4e07ba959d7418c32a9dd8d698b8f107e2d5eae92ebf66e1373"},"platforms":{"qwindows.dll":"9ddfa666be52fe9b2fa1b5421a6d3d029eafe309bd971d0ab295c67da7bc6e14"},"Qt6Core.dll":"ba5e271f3052ff23e3d74a6462c93f54d6a11e08b3312a726992bf39b5160125","Qt6Gui.dll":"4995adb2bc21deaa123263638bc6fa0b504ac3a68e0bf7c7640caef6cfba0b71","Qt6Network.dll":"6f4a21e363a6d144eedeefb0b4c7726e036c1be3533487846748fadccc07801a","Qt6OpenGL.dll":"ebb6472d1c7c8b960aaf6492b51b841e527be9f6f9da51705a5fd6d537828e09","Qt6OpenGLWidgets.dll":"769d7906ceddd86d397bc4a1004833b3599e55cf978cf9c1f7d3ff0a8b32589d","Qt6RemoteObjects.dll":"c8c2aa99af3ee210cb8c560f655464132a8030d327a27774cec97a92f4a64b98","Qt6Svg.dll":"a21df38a3a8a4e9303cfd0823ce4bf234f1c676b84d896b94051c03873a146f0","Qt6UiTools.dll":"260d7526ca48905827101162ce030ffd5286a5cd8818191014516ef732432564","Qt6Widgets.dll":"e36a443c014c447fd325235292a4b0ad84c5ff4fbcbc78c6cdd76b393100e20b","Qt6Xml.dll":"ed307a0cc4629568509015cda6a545e32f294f21dd9f9be22ad1b8b191e703dd","styles":{"qwindowsvistastyle.dll":"16be061c545a0a9fd9444e776ab99c3353e24d99b7a6524b3343d72e3e64a220"},"tls":{"qcertonlybackend.dll":"9c919ec08a2b8ce2895d29b6e253765b5b0ccab23033f00d9bb345e2c3095e74","qopensslbackend.dll":"50079800b0c310a0b7cb249c4296932b3926dae2d8a3cc24c2be5ee87dc4070f","qschannelbackend.dll":"eeb4d3e5ee554c85da49caeb7e396dbc8c266506a7ae5431ddb5678cf60e5e04"},"translations":{}}})";
    QJsonDocument jsonDocument = QJsonDocument::fromJson(jsonString.toUtf8());
    if (!jsonDocument.isObject()) 
        qDebug() << "Invalid JSON format";

    QJsonObject jsonObject = jsonDocument.object();
    QMap<QString, QString> map;
    parseJsonObject(jsonObject, map);   
    return a.exec();
}
C++ JSON QT系列

评论

0赞 JarMan 8/26/2023
您尚未定义代码中遇到的问题。预期输出是多少,您实际得到了什么?您是否尝试过使用调试器逐步执行代码以查看出错的地方?
0赞 JarMan 8/26/2023
我注意到的一件事是,您没有检查数组中的数组,但也许这对您的用例来说是可以的。

答: 暂无答案