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. Qt5 Quickly convert json to an object without converting field by field
Forum Updated to NodeBB v4.3 + New Features

Qt5 Quickly convert json to an object without converting field by field

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 5 Posters 997 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.
  • Christian EhrlicherC Christian Ehrlicher

    And what should setId() do? How should it magically know where to put the value to?

    X Offline
    X Offline
    Xrtero
    wrote on last edited by
    #5

    @Christian-Ehrlicher
    just set my object field

    .....
    in MyObject class 
    id is MyObject field
    .....
    void setId(quint32 newId){
    id = newId
    }
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Online
      Christian EhrlicherC Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #6

      Sorry I don't understand the problem.
      What's wrong with

      myObject.setId(json["data"]["id"].toInt());

      ?

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • X Xrtero

        @Christian-Ehrlicher said in Qt5 Quickly convert json to an object without converting field by field:

        I don't see how this should be done quicker - you have to pass the keys somehow.

        like this:
        a http get request .

        ......
        In http request
        .....
        QNetworkReply* reply; /*initialized*/
        
        QByteArray      data = reply->readAll().data();
        QJsonParseError jsonError;
        QJsonDocument   json = QJsonDocument::fromJson(data, &jsonError);
        
        MyObject* myObject = MyObject::fromJson(json.object());
        
        ......
        In MyObject  fromJson function
        .....
        myObject.setId(json["data"]["id"].toInt());
        
        
        
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #7

        @Xrtero
        Like @Christian-Ehrlicher I'm not sure what your issue is.

        You are apparently receiving the JSON text over the network. I would assume that will take waayyy longer than any in-memory manipulation anyway, and the second-most expensive (though far behind) might be the fromJson() text parsing.

        X 1 Reply Last reply
        0
        • JonBJ JonB

          @Xrtero
          Like @Christian-Ehrlicher I'm not sure what your issue is.

          You are apparently receiving the JSON text over the network. I would assume that will take waayyy longer than any in-memory manipulation anyway, and the second-most expensive (though far behind) might be the fromJson() text parsing.

          X Offline
          X Offline
          Xrtero
          wrote on last edited by Xrtero
          #8

          @Christian-Ehrlicher said in Qt5 Quickly convert json to an object without converting field by field:

          Sorry I don't understand the problem.
          What's wrong with

          myObject.setId(json["data"]["id"].toInt());

          ?

          @JonB said in Qt5 Quickly convert json to an object without converting field by field:

          @Xrtero
          Like @Christian-Ehrlicher I'm not sure what your issue is.

          You are apparently receiving the JSON text over the network. I would assume that will take waayyy longer than any in-memory manipulation anyway, and the second-most expensive (though far behind) might be the fromJson() text parsing.

          Send an HTTP GET request using QNetworkAccessManager and receive a response in the format of Application/json. I need to parse this response (JSON) into an object.

          Pl45m4P Christian EhrlicherC 2 Replies Last reply
          0
          • X Xrtero

            @Christian-Ehrlicher said in Qt5 Quickly convert json to an object without converting field by field:

            Sorry I don't understand the problem.
            What's wrong with

            myObject.setId(json["data"]["id"].toInt());

            ?

            @JonB said in Qt5 Quickly convert json to an object without converting field by field:

            @Xrtero
            Like @Christian-Ehrlicher I'm not sure what your issue is.

            You are apparently receiving the JSON text over the network. I would assume that will take waayyy longer than any in-memory manipulation anyway, and the second-most expensive (though far behind) might be the fromJson() text parsing.

            Send an HTTP GET request using QNetworkAccessManager and receive a response in the format of Application/json. I need to parse this response (JSON) into an object.

            Pl45m4P Offline
            Pl45m4P Offline
            Pl45m4
            wrote on last edited by
            #9

            @Xrtero

            Maybe this topic helps:

            • https://stackoverflow.com/questions/19822211/qt-parsing-json-using-qjsondocument-qjsonobject-qjsonarray

            If debugging is the process of removing software bugs, then programming must be the process of putting them in.

            ~E. W. Dijkstra

            1 Reply Last reply
            0
            • X Xrtero

              @Christian-Ehrlicher said in Qt5 Quickly convert json to an object without converting field by field:

              Sorry I don't understand the problem.
              What's wrong with

              myObject.setId(json["data"]["id"].toInt());

              ?

              @JonB said in Qt5 Quickly convert json to an object without converting field by field:

              @Xrtero
              Like @Christian-Ehrlicher I'm not sure what your issue is.

              You are apparently receiving the JSON text over the network. I would assume that will take waayyy longer than any in-memory manipulation anyway, and the second-most expensive (though far behind) might be the fromJson() text parsing.

              Send an HTTP GET request using QNetworkAccessManager and receive a response in the format of Application/json. I need to parse this response (JSON) into an object.

              Christian EhrlicherC Online
              Christian EhrlicherC Online
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #10

              @Xrtero said in Qt5 Quickly convert json to an object without converting field by field:

              . I need to parse this response (JSON) into an object.

              You already do this...

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              X 1 Reply Last reply
              1
              • Christian EhrlicherC Christian Ehrlicher

                @Xrtero said in Qt5 Quickly convert json to an object without converting field by field:

                . I need to parse this response (JSON) into an object.

                You already do this...

                X Offline
                X Offline
                Xrtero
                wrote on last edited by
                #11

                @Christian-Ehrlicher

                Therefore, I'm wondering if there's a more convenient method. For instance, if the JSON is large and the object to be converted has many fields, manually writing the code would involve a significant amount of work.

                JonBJ 1 Reply Last reply
                0
                • Christian EhrlicherC Online
                  Christian EhrlicherC Online
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12

                  What should be more convenient here? How should some magic code know which attribute you want? Noone else but you can know what you want to retrieve from the json.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  X 1 Reply Last reply
                  1
                  • X Xrtero

                    @Christian-Ehrlicher

                    Therefore, I'm wondering if there's a more convenient method. For instance, if the JSON is large and the object to be converted has many fields, manually writing the code would involve a significant amount of work.

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #13

                    @Xrtero IMO you are worrying about something you should not be worrying about.

                    X 1 Reply Last reply
                    0
                    • hskoglundH Offline
                      hskoglundH Offline
                      hskoglund
                      wrote on last edited by hskoglund
                      #14

                      With a bit of luck reflection will be included in C++26 and then you can write something like this:

                      MyObject myObject;
                      for (auto d : std::meta::nonstatic_data_members_of(^MyObject))
                          myObject.[:d:] = json["data"][[:d:]].toInt();
                      

                      I.e. steps through all your different data members in a loop (assuming they are all of the type int).
                      More info here

                      X 1 Reply Last reply
                      0
                      • Christian EhrlicherC Christian Ehrlicher

                        What should be more convenient here? How should some magic code know which attribute you want? Noone else but you can know what you want to retrieve from the json.

                        X Offline
                        X Offline
                        Xrtero
                        wrote on last edited by
                        #15

                        @Christian-Ehrlicher said in Qt5 Quickly convert json to an object without converting field by field:

                        What should be more convenient here? How should some magic code know which attribute you want? Noone else but you can know what you want to retrieve from the json.

                        My current idea is to implement this feature using Qt's reflection, which would allow us to access each field and convert it into the corresponding type.

                        1 Reply Last reply
                        0
                        • JonBJ JonB

                          @Xrtero IMO you are worrying about something you should not be worrying about.

                          X Offline
                          X Offline
                          Xrtero
                          wrote on last edited by
                          #16

                          @JonB

                          just to make convert json convenient and reduce some repetitive work.

                          1 Reply Last reply
                          0
                          • hskoglundH hskoglund

                            With a bit of luck reflection will be included in C++26 and then you can write something like this:

                            MyObject myObject;
                            for (auto d : std::meta::nonstatic_data_members_of(^MyObject))
                                myObject.[:d:] = json["data"][[:d:]].toInt();
                            

                            I.e. steps through all your different data members in a loop (assuming they are all of the type int).
                            More info here

                            X Offline
                            X Offline
                            Xrtero
                            wrote on last edited by
                            #17

                            @hskoglund said in Qt5 Quickly convert json to an object without converting field by field:

                            With a bit of luck reflection will be included in C++26 and then you can write something like this:

                            MyObject myObject;
                            for (auto d : std::meta::nonstatic_data_members_of(^MyObject))
                                myObject.[:d:] = json["data"][[:d:]].toInt();
                            

                            I.e. steps through all your different data members in a loop (assuming they are all of the type int).
                            More info here

                            Qt also has the same feature, and that's what I was planning to do.
                            Finally, someone understands my idea. Thank you!

                            JonBJ 1 Reply Last reply
                            0
                            • X Xrtero

                              @hskoglund said in Qt5 Quickly convert json to an object without converting field by field:

                              With a bit of luck reflection will be included in C++26 and then you can write something like this:

                              MyObject myObject;
                              for (auto d : std::meta::nonstatic_data_members_of(^MyObject))
                                  myObject.[:d:] = json["data"][[:d:]].toInt();
                              

                              I.e. steps through all your different data members in a loop (assuming they are all of the type int).
                              More info here

                              Qt also has the same feature, and that's what I was planning to do.
                              Finally, someone understands my idea. Thank you!

                              JonBJ Offline
                              JonBJ Offline
                              JonB
                              wrote on last edited by
                              #18

                              @Xrtero
                              So you will use a feature of C++ 26, and it will only work if all your properties are of type int. You could do something like this easily if you changed to a language like Python. Anyway if this is what you are looking for and you are happy that is good.

                              1 Reply Last reply
                              0
                              • X Xrtero has marked this topic as solved on

                              • Login

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