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 987 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.
  • 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 Offline
      Christian EhrlicherC Offline
      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 Offline
          Christian EhrlicherC Offline
          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 Online
            JonBJ Online
            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 Online
              hskoglundH Online
              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 Online
                      JonBJ Online
                      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