Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to Create dynamically JSON object in QT ?
Forum Update on Monday, May 27th 2025

How to Create dynamically JSON object in QT ?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
8 Posts 4 Posters 5.8k Views
  • 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.
  • M Offline
    M Offline
    Mayank Thakor
    wrote on 15 Feb 2018, 07:47 last edited by Mayank Thakor
    #1

    I need to insert the value in that JSON object.

    give some reference link or documents so i will create dynamically JSON object in my qt creator.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      sierdzio
      Moderators
      wrote on 15 Feb 2018, 08:17 last edited by
      #2

      https://doc.qt.io/qt-5/json.html

      (Z(:^

      1 Reply Last reply
      1
      • M Offline
        M Offline
        Mayank Thakor
        wrote on 15 Feb 2018, 09:03 last edited by
        #3

        correct
        But i need to create multiple JSON message dynamically
        and from that multiple JSON messages
        i need to filler that JSON message base on their property type and properties

        so How i will create JSON messages and how i will add different property type in JSON ??

        1 Reply Last reply
        0
        • S Offline
          S Offline
          sierdzio
          Moderators
          wrote on 15 Feb 2018, 11:59 last edited by
          #4

          I'm sorry but I don't understand what you want to achieve. Creating a simple JSON message is something akin to:

          QJsonObject obj;
          obj.insert("someKey", "someValue");
          obj.insert("someOtherKey", 123);
          obj.insert("array", QStringList() {"a", "b"});
          QJsonDocument doc(obj).toJson(); // <- this is your JSON string
          

          To create multiple messages, just repeat the process... I don't see a problem here. But then again, as said, I don't understand what your question really is about.

          (Z(:^

          1 Reply Last reply
          1
          • M Offline
            M Offline
            Mayank Thakor
            wrote on 15 Feb 2018, 12:06 last edited by Mayank Thakor
            #5

            THANK YOU
            i want like this and i understand this

            QJsonObject obj;
            obj.insert("someKey", "someValue");
            obj.insert("someOtherKey", 123);
            obj.insert("array", QStringList() {"a", "b"});
            QJsonDocument doc(obj).toJson(); // <- this is your JSON string

            but

            I want to create JSON object dynamically (with new ) and i want to give TYPE and other property.

            for example
            one jason message it's TYPE is "jason_type_1" and it has 2 property like "jason_1_property_1" and "jason_property_2".

            second jason message it's TYPE is "jason_type_2" and it has 3 property like "jason_2_property_1" , "jason_2_property_2" and "jason_3_property_3".

            so base on the TYPE "jason_type_1" or "jason_type_2" i can filter the message and
            then i read property like
            QString read_string = (jsonObj["body"].toObject()["jason_1_property_1"]).toString();

            so can you please give example how i will create this in QT ?

            1 Reply Last reply
            0
            • O Offline
              O Offline
              ODБOï
              wrote on 15 Feb 2018, 17:19 last edited by ODБOï
              #6

              hi @Mayank-Thakor,

              Im not sure but, would it be possible to insert your 'TYPE ' and 'property ' directly in the json obj itself ?

              QJsonObject obj;
              obj.insert("TYPE", "jason_type_1"); // type
              obj.insert("prop1", jason_1_property_1); // property 1
              obj.insert("prop1", jason_1_property_2); // property 2
              obj.insert("array", QStringList() {"a", "b"});
              QJsonDocument doc(obj).toJson(); // <- this is your JSON string
              

              Edit : also look this http://doc.qt.io/qt-5/qtqml-javascript-dynamicobjectcreation.html

              1 Reply Last reply
              0
              • M Offline
                M Offline
                Mayank Thakor
                wrote on 16 Feb 2018, 05:56 last edited by
                #7

                @LeLev said in How to Create dynamically JSON object in QT ?:

                QStringList

                Okay got it

                Thank you !

                1 Reply Last reply
                1
                • P Offline
                  P Offline
                  Pablo J. Rogina
                  wrote on 16 Feb 2018, 13:35 last edited by
                  #8

                  @Mayank-Thakor if your issue is finally solved, please don't forget to mark your post as such! thank you.

                  Upvote the answer(s) that helped you solve the issue
                  Use "Topic Tools" button to mark your post as Solved
                  Add screenshots via postimage.org
                  Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply
                  0

                  1/8

                  15 Feb 2018, 07:47

                  • Login

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