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. Multiple JSON objects to one JSON object
Forum Updated to NodeBB v4.3 + New Features

Multiple JSON objects to one JSON object

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 3 Posters 2.6k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on 20 May 2019, 08:52 last edited by
    #1

    While parsing INI file, I got various QVariants which I converted to Json objects using the following code:

    QSettings settings("test.ini",QSettings::IniFormat);
    
    QStringList groups= settings.childGroups();
    
    foreach (QString group, groups)
    {
    
        qDebug() << group;
    
        settings.beginGroup(group);
    
        QStringList keys = settings.childKeys();
    
        foreach (key, keys) {
    
            values=settings.value(key);
    
            qDebug() << values;
    
    
            QJsonObject object;
    
            QJsonValue valueJson(values.toString());
    
            object.insert(key,valueJson);
    
            qDebug()<<object;
    
        }
    
    settings.endGroup();
    

    Now I have multiple QVariants and QJsonObjects like this:

    QVariant(QString, "value1")
    
    QJsonObject({"name1":"value1"})
    
    QVariant(QString, "value2")
    
    QJsonObject({"name2":"value2"})
    
    QVariant(QString, "value3")
    
    QJsonObject({"name3":"value3"})
    

    How can I get one JsonObject that looks like this:

    QJsonObject({"name1":"value1"},{"name2":"value2"},{"name3":"value3"})
    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on 20 May 2019, 12:09 last edited by
      #5

      Here is the latest I've tried:

      QSettings settings("test.ini",QSettings::IniFormat);
      QStringList groups= settings.childGroups(); 
      
      foreach (QString group, groups)
      { 
          qDebug() << group; 
          settings.beginGroup(group); 
          QStringList keys = settings.childKeys(); 
      
          foreach (key, keys) { 
              values=settings.value(key); 
              qDebug() << values; 
               QJsonObject object;
               QJsonValue valueJson(values.toString());
               object.insert(key,valueJson);
               qDebug()<<object;
      
      foreach (valueJson, object) {
                         QJsonArray arr;
                         arr.append(object);
                         qDebug()<<arr;
                     }
           }
       settings.endGroup();
      }
      
      1 Reply Last reply
      0
      • V Offline
        V Offline
        VRonin
        wrote on 20 May 2019, 09:59 last edited by
        #2

        That's not a QJsonObject it's a QJsonArray. You just append the 3 objects to the array

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on 20 May 2019, 11:54 last edited by
          #3

          Thank you for the suggestion. I've tried something like this:

          QJsonArray arr;
          arr.append(object);
          qDebug()<<arr;
          

          but whatever I do, I get QJsonArray with only one Json Object and the output looks like this:

          QVariant(QString, "value1")
          QJsonObject({"name1":"value1"})
          QJsonArray([{"name1":"value1"}])
          QVariant(QString, "value2")
          QJsonObject({"name2":"value2"})
          QJsonArray([{"name2":"value2"}])
          QVariant(QString, "value3")
          QJsonObject({"name3":"value3"})
          QJsonArray([{"name3":"value3"}])
          

          I know I have to use for loop somehow, but I cant get it right.

          1 Reply Last reply
          0
          • V Offline
            V Offline
            VRonin
            wrote on 20 May 2019, 12:01 last edited by
            #4

            can you post your code?

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on 20 May 2019, 12:09 last edited by
              #5

              Here is the latest I've tried:

              QSettings settings("test.ini",QSettings::IniFormat);
              QStringList groups= settings.childGroups(); 
              
              foreach (QString group, groups)
              { 
                  qDebug() << group; 
                  settings.beginGroup(group); 
                  QStringList keys = settings.childKeys(); 
              
                  foreach (key, keys) { 
                      values=settings.value(key); 
                      qDebug() << values; 
                       QJsonObject object;
                       QJsonValue valueJson(values.toString());
                       object.insert(key,valueJson);
                       qDebug()<<object;
              
              foreach (valueJson, object) {
                                 QJsonArray arr;
                                 arr.append(object);
                                 qDebug()<<arr;
                             }
                   }
               settings.endGroup();
              }
              
              1 Reply Last reply
              0
              • V Offline
                V Offline
                VRonin
                wrote on 20 May 2019, 12:12 last edited by
                #6

                the array goes outside:

                QSettings settings("test.ini",QSettings::IniFormat);
                QStringList groups= settings.childGroups(); 
                
                QJsonObject iniObj;
                foreach (QString group, groups)
                { 
                    settings.beginGroup(group); 
                    QStringList keys = settings.childKeys(); 
                QJsonArray keys;
                    foreach (key, keys) { 
                        values=settings.value(key); 
                         QJsonObject object;
                         QJsonValue valueJson(values.toString());
                         object.insert(key,valueJson);
                        keys.append(object);
                
                     }
                iniObj.insert(group,keys);
                 settings.endGroup();
                }
                

                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                ~Napoleon Bonaparte

                On a crusade to banish setIndexWidget() from the holy land of Qt

                1 Reply Last reply
                3
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on 20 May 2019, 12:20 last edited by
                  #7

                  Omg, that's right. Thank you so much!

                  1 Reply Last reply
                  0
                  • JKSHJ Offline
                    JKSHJ Offline
                    JKSH
                    Moderators
                    wrote on 21 May 2019, 04:18 last edited by
                    #8

                    @YouKnowMe Please leave the messages (don't delete them) after you find a solution. This allows other people to read your problem and benefit from the solution.

                    I have restored the deleted messages.

                    Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                    1 Reply Last reply
                    0

                    1/8

                    20 May 2019, 08:52

                    • 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