Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Parse json string to QMap
Forum Updated to NodeBB v4.3 + New Features

Parse json string to QMap

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 3 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rua3n
    wrote on 25 Aug 2023, 19:33 last edited by
    #1

    I'm not figuring out how to properly parse a JSON string and transform it into a QMap<QString, QString> my doubt relies on the part of checking if something is an array or object and recursively iterate it adding each value to its correspondent key/parent object.

    The way I did the map is being constructed like this:

    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();
    }
    
    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Aug 2023, 19:38 last edited by
      #2

      Hi,

      What exactly is your doubt ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      R 1 Reply Last reply 25 Aug 2023, 19:45
      0
      • S SGaist
        25 Aug 2023, 19:38

        Hi,

        What exactly is your doubt ?

        R Offline
        R Offline
        Rua3n
        wrote on 25 Aug 2023, 19:45 last edited by
        #3

        @SGaist how to properly construct the QMap from this json string.

        S 1 Reply Last reply 25 Aug 2023, 19:58
        0
        • R Rua3n
          25 Aug 2023, 19:45

          @SGaist how to properly construct the QMap from this json string.

          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 25 Aug 2023, 19:58 last edited by
          #4

          Ok, I see. Your main issue is that you are doing your work always in the same map. Once you have an object, you have to create a new map, fill it and then assign it to the current one. But ! You can't assign a QMap value to your QMap<QString, QString>. So your original plan can't work.

          That's why QJsonObject as the toVariantMap function.

          You have to change your design to work with QVariantMap.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          R 1 Reply Last reply 25 Aug 2023, 20:05
          1
          • S SGaist
            25 Aug 2023, 19:58

            Ok, I see. Your main issue is that you are doing your work always in the same map. Once you have an object, you have to create a new map, fill it and then assign it to the current one. But ! You can't assign a QMap value to your QMap<QString, QString>. So your original plan can't work.

            That's why QJsonObject as the toVariantMap function.

            You have to change your design to work with QVariantMap.

            R Offline
            R Offline
            Rua3n
            wrote on 25 Aug 2023, 20:05 last edited by
            #5

            @SGaist but im trying to build a QMap<Qstring, QString> not QVariant

            S 1 Reply Last reply 25 Aug 2023, 20:14
            0
            • R Rua3n
              25 Aug 2023, 20:05

              @SGaist but im trying to build a QMap<Qstring, QString> not QVariant

              S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 25 Aug 2023, 20:14 last edited by
              #6

              Then I may have misunderstood your current issue.

              Can you show an example of the input you get and the output you expect to build from it ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              R 1 Reply Last reply 25 Aug 2023, 20:31
              0
              • S SGaist
                25 Aug 2023, 20:14

                Then I may have misunderstood your current issue.

                Can you show an example of the input you get and the output you expect to build from it ?

                R Offline
                R Offline
                Rua3n
                wrote on 25 Aug 2023, 20:31 last edited by
                #7

                @SGaist

                QMap<QString, QMap<QString, QMap<QString, QString>>> m = {
                        {"files", {
                            {"App.exe", {{"hash", "f976"}}},
                            {"D3Dcompiler_47.dll", {{"hash", "954f"}}},
                            {"generic", {
                                {"qtuiotouchplugin.dll", {{"hash", "ed72"}}}, {"test", {{"hash", ""}}},
                            }}
                        }}
                    };
                

                There's another way to convert the json back to object/array?

                S 1 Reply Last reply 25 Aug 2023, 20:44
                0
                • R Rua3n
                  25 Aug 2023, 20:31

                  @SGaist

                  QMap<QString, QMap<QString, QMap<QString, QString>>> m = {
                          {"files", {
                              {"App.exe", {{"hash", "f976"}}},
                              {"D3Dcompiler_47.dll", {{"hash", "954f"}}},
                              {"generic", {
                                  {"qtuiotouchplugin.dll", {{"hash", "ed72"}}}, {"test", {{"hash", ""}}},
                              }}
                          }}
                      };
                  

                  There's another way to convert the json back to object/array?

                  S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 25 Aug 2023, 20:44 last edited by
                  #8

                  You can't dynamically create that kind of class in C++. However, if you have such a fixed known structure, then my remark about creating a new QMap each time you have to recurse.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  R 1 Reply Last reply 25 Aug 2023, 20:53
                  1
                  • S SGaist
                    25 Aug 2023, 20:44

                    You can't dynamically create that kind of class in C++. However, if you have such a fixed known structure, then my remark about creating a new QMap each time you have to recurse.

                    R Offline
                    R Offline
                    Rua3n
                    wrote on 25 Aug 2023, 20:53 last edited by
                    #9

                    @SGaist example please

                    S 1 Reply Last reply 25 Aug 2023, 20:59
                    0
                    • R Rua3n
                      25 Aug 2023, 20:53

                      @SGaist example please

                      S Offline
                      S Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 25 Aug 2023, 20:59 last edited by
                      #10

                      @Rua3n example of what ?

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      S 1 Reply Last reply 26 Aug 2023, 13:14
                      0
                      • S SGaist
                        25 Aug 2023, 20:59

                        @Rua3n example of what ?

                        S Offline
                        S Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on 26 Aug 2023, 13:14 last edited by
                        #11

                        I just realized something, based on what you want, you might want to check hlohmann json library. It might be better suited to your needs.

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        J 1 Reply Last reply 26 Aug 2023, 14:23
                        0
                        • S SGaist
                          26 Aug 2023, 13:14

                          I just realized something, based on what you want, you might want to check hlohmann json library. It might be better suited to your needs.

                          J Offline
                          J Offline
                          JonB
                          wrote on 26 Aug 2023, 14:23 last edited by JonB
                          #12

                          @SGaist
                          Blimey! From where I am/OP is now, I would just work with QJson and QVariantMap!

                          S 1 Reply Last reply 26 Aug 2023, 16:07
                          0
                          • J JonB
                            26 Aug 2023, 14:23

                            @SGaist
                            Blimey! From where I am/OP is now, I would just work with QJson and QVariantMap!

                            S Offline
                            S Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on 26 Aug 2023, 16:07 last edited by
                            #13

                            @JonB said in Parse json string to QMap:

                            @SGaist
                            Blimey! From where I am/OP is now, I would just work with QJson and QVariantMap!

                            I already suggested that, but it seems not to be an option.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            J 1 Reply Last reply 26 Aug 2023, 19:02
                            1
                            • S SGaist
                              26 Aug 2023, 16:07

                              @JonB said in Parse json string to QMap:

                              @SGaist
                              Blimey! From where I am/OP is now, I would just work with QJson and QVariantMap!

                              I already suggested that, but it seems not to be an option.

                              J Offline
                              J Offline
                              JonB
                              wrote on 26 Aug 2023, 19:02 last edited by
                              #14

                              @SGaist
                              I know, I was agreeing with you :)

                              No idea how much JSON data OP has to parse. They might read it in as QVariantMap and transform in code to some specific QMap<..., ...> if demanded. Not the fastest, but might be quickest if OP wants to write it. Integrating the code you referenced has its own overheads :)

                              1 Reply Last reply
                              0

                              8/14

                              25 Aug 2023, 20:44

                              • Login

                              • Login or register to search.
                              8 out of 14
                              • First post
                                8/14
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved