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. Is there a simpler way of building and modifying a QJsonObject?
QtWS25 Last Chance

Is there a simpler way of building and modifying a QJsonObject?

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 7 Posters 924 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.
  • N Offline
    N Offline
    need4openid
    wrote on 23 Mar 2020, 13:29 last edited by
    #1

    For example, I would like to build the following json (its a simple one for the sake of the sample, in real life I have to construct much larger objects):

    {
        "person":{
            "salary": {
                "min": 10000,
                "max": 20000
            }
        }
    }
    

    I build this object something like this (there might be syntax errors, I did not check):

    QJsonObject({{"person", QJsonObject({{"salary", QJsonObject({{"min", 10000}, {"max", 20000}})}})}})
    

    It can get quite ugly. Is there a better way of constructing such JsonOBject? The min/max values I obtain them dynamically at run time, so they cannot be imported with QJsonDocument::fromJson just like that.

    J 1 Reply Last reply 23 Mar 2020, 13:30
    0
    • N need4openid
      23 Mar 2020, 13:29

      For example, I would like to build the following json (its a simple one for the sake of the sample, in real life I have to construct much larger objects):

      {
          "person":{
              "salary": {
                  "min": 10000,
                  "max": 20000
              }
          }
      }
      

      I build this object something like this (there might be syntax errors, I did not check):

      QJsonObject({{"person", QJsonObject({{"salary", QJsonObject({{"min", 10000}, {"max", 20000}})}})}})
      

      It can get quite ugly. Is there a better way of constructing such JsonOBject? The min/max values I obtain them dynamically at run time, so they cannot be imported with QJsonDocument::fromJson just like that.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 23 Mar 2020, 13:30 last edited by
      #2

      @need4openid Depending on your exact use case you can read JSON from a string.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • S Offline
        S Offline
        SGaist
        Lifetime Qt Champion
        wrote on 23 Mar 2020, 13:33 last edited by
        #3

        Hi,

        Depending on what you would like to do you might also want to consider nlohmann's json library which is quite good.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        2
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 23 Mar 2020, 14:30 last edited by mrjj
          #4

          Hi
          if you are using a c++ 11 compiler you can also use raw string literals.

           QString jsontemplate = R"(
              {
                  "person":{
                      "salary": {
                          "min": %1,
                          "max": %2
                      }
                  }
              })";
          
           QString finaleJson = jsontemplate.arg(1000).arg(2000);
          
           qDebug() << finaleJson;
          
          

          ps. +5 for nlohmann's json. Its awesome.

          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 23 Mar 2020, 14:43 last edited by
            #5

            @need4openid said in Is there a simpler way of building and modifying a QJsonObject?:

            QJsonObject({{"person", QJsonObject({{"salary", QJsonObject({{"min", 10000}, {"max", 20000}})}})}})

            QJsonValue Foo::getValues() const
            {
              return {{"person", m_person},
                      {"salary", m_salary},
                      {"min", m_min},
                      {"max", m_max},
                     }
            

            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
            2
            • M Offline
              M Offline
              MrShawn
              wrote on 25 Mar 2020, 18:14 last edited by
              #6

              I like to use the insert method.

                  QJsonObject person;
                  person.insert("min",10000);
                  person.insert("max",20000);
                  QJsonObject object;
                  object.insert("person",person);
              
                  QJsonDocument doc;
                  doc.setObject(object);
                  qDebug() << doc.toJson(QJsonDocument::Compact);
              
              1 Reply Last reply
              1
              • T Offline
                T Offline
                Trzmiel
                wrote on 25 Mar 2020, 20:00 last edited by
                #7

                If it is more convenient for you to manage the data in a collection (map or hash table), there are methods to create QJsonObject from QVariantMap and QVaraintHash.

                int min = 10000, max = 20000;
                
                QVariantMap salary;
                salary["max"] = max;
                salary["min"] = min;
                
                QVariantMap person;
                person["salary"] = salary;
                
                QVariantMap jsonMap;
                jsonMap["person"] = person;
                
                QJsonDocument jsonDoc(QJsonObject::fromVariantMap(jsonMap));
                qDebug() << jsonDoc.toJson(QJsonDocument::Compact);
                
                1 Reply Last reply
                2

                5/7

                23 Mar 2020, 14:43

                • Login

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