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.6k 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 Rua3n

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

    SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on 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
    1
    • SGaistS SGaist

      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 last edited by
      #5

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

      SGaistS 1 Reply Last reply
      0
      • R Rua3n

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

        SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on 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
        0
        • SGaistS SGaist

          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 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?

          SGaistS 1 Reply Last reply
          0
          • R Rua3n

            @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?

            SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 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
            1
            • SGaistS SGaist

              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 last edited by
              #9

              @SGaist example please

              SGaistS 1 Reply Last reply
              0
              • R Rua3n

                @SGaist example please

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on 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

                SGaistS 1 Reply Last reply
                0
                • SGaistS SGaist

                  @Rua3n example of what ?

                  SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 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

                  JonBJ 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    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.

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by JonB
                    #12

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

                    SGaistS 1 Reply Last reply
                    0
                    • JonBJ JonB

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

                      SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 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

                      JonBJ 1 Reply Last reply
                      1
                      • SGaistS SGaist

                        @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.

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on 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

                        • Login

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