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. How to create JSON using QJsonDocument?

How to create JSON using QJsonDocument?

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 964 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.
  • A Offline
    A Offline
    ajaxcrypto
    wrote on last edited by
    #1

    Here is a small snippet

       QJsonValue value{QJsonValue::Object};
       value.toObject().insert("key", "value");
       QJsonDocument doc;
       doc.setObject(value.toObject());
       std::cout << doc.toJson().toStdString();
    

    It however outputs a blank object i.e. {} in console. Why is that the case?

    JonBJ 1 Reply Last reply
    0
    • A ajaxcrypto

      Here is a small snippet

         QJsonValue value{QJsonValue::Object};
         value.toObject().insert("key", "value");
         QJsonDocument doc;
         doc.setObject(value.toObject());
         std::cout << doc.toJson().toStdString();
      

      It however outputs a blank object i.e. {} in console. Why is that the case?

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

      @ajaxcrypto said in How to create JSON using QJsonDocument?:

         QJsonValue value{QJsonValue::Object};
         value.toObject().insert("key", "value");
      

      Does this alter your QJsonValue value ?

      aha_1980A 1 Reply Last reply
      1
      • JonBJ JonB

        @ajaxcrypto said in How to create JSON using QJsonDocument?:

           QJsonValue value{QJsonValue::Object};
           value.toObject().insert("key", "value");
        

        Does this alter your QJsonValue value ?

        aha_1980A Offline
        aha_1980A Offline
        aha_1980
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @JNBarchan said in How to create JSON using QJsonDocument?:

        @ajaxcrypto said in How to create JSON using QJsonDocument?:

           QJsonValue value{QJsonValue::Object};
           value.toObject().insert("key", "value");
        

        Does this alter your QJsonValue value ?

        No.

        @ajaxcrypto: Try this:

            QJsonValue value{QJsonValue::Object};
            QJsonObject object = value.toObject();
            object.insert("key", "value");
            QJsonDocument doc;
            doc.setObject(object);
            std::cout << doc.toJson().toStdString();
        

        Qt has to stay free or it will die.

        1 Reply Last reply
        3

        • Login

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